WebSocket endpoints for Express applications. Express应用程序的WebSocket端点。Lets you define WebSocket endpoints like any other type of route, and applies regular Express middleware. 允许您像任何其他类型的路由一样定义WebSocket端点,并应用常规Express中间件。The WebSocket support is implemented with the help of the ws library.WebSocket支持是在ws库的帮助下实现的。
npm install --save express-ws
Full documentation can be found in the API section below. This section only shows a brief example.完整的文档可以在下面的API部分中找到。本节仅显示一个简短的示例。
Add this line to your Express application:将此行添加到您的Express应用程序中:
var expressWs = require('express-ws')(app);
Important: Make sure to set up the 重要提示:在加载或定义路由器之前,请确保像上面一样设置express-ws
module like above before loading or defining your routers!express-ws
模块! Otherwise, express-ws
won't get a chance to set up support for Express routers, and you might run into an error along the lines of router.ws is not a function
.否则,express-ws
将没有机会设置对express路由器的支持,并且您可能会在路由器上遇到router.ws is not a function
错误。
After setting up 在设置了express-ws
, you will be able to add WebSocket routes (almost) the same way you add other routes. express-ws
之后,您将能够添加WebSocket路由(几乎),就像添加其他路由一样。The following snippet sets up a simple echo server at 下面的代码段在/echo
. /echo
中设置了一个简单的echo服务器。The ws参数是这里描述的WebSocket类的一个实例。ws
parameter is an instance of the WebSocket class described here.
app.ws('/echo', function(ws, req) {
ws.on('message', function(msg) {
ws.send(msg);
});
});
It works with routers, too, this time at 它也适用于路由器,这次是在/ws-stuff/echo
:/ws-stuff/echo
:
var router = express.Router();
router.ws('/echo', function(ws, req) {
ws.on('message', function(msg) {
ws.send(msg);
});
});
app.use("/ws-stuff", router);
var express = require('express');
var app = express();
var expressWs = require('express-ws')(app);
app.use(function (req, res, next) {
console.log('middleware');
req.testing = 'testing';
return next();
});
app.get('/', function(req, res, next){
console.log('get route', req.testing);
res.end();
});
app.ws('/', function(ws, req) {
ws.on('message', function(msg) {
console.log(msg);
});
console.log('socket', req.testing);
});
app.listen(3000);
Sets up 在指定的express-ws
on the specified app
. app
上设置express-ws
。This will modify the global Router prototype for Express as well - see the 这也将修改Express的全局路由器原型-有关禁用此功能的更多信息,请参阅leaveRouterUntouched
option for more information on disabling this.leaveRouterUntouched
选项。
express-ws
on.express-ws
的Express应用程序。http.Server
, you should pass it in here, so that express-ws
can use it to set up the WebSocket upgrade handlers. http.Server
时,应该将其传递到此处,以便express ws可以使用它来设置WebSocket升级处理程序。server
, you will only be able to use it with the server that is created automatically when you call app.listen
.server
,则只能将其与调用app.listen
时自动创建的服务器一起使用。true
to keep express-ws
from modifying the Router prototype. true
可防止express-ws
修改Router原型。applyTo
every Router that you wish to make .ws
available on, when this is enabled.This function will return a new 此函数将返回一个新的express-ws
API object, which will be referred to as wsInstance
in the rest of the documentation.express-ws
API对象,在文档的其余部分中称为wsInstance
。
This property contains the 此属性包含在其上设置app
that express-ws
was set up on.express-ws
的app
。
Returns the underlying WebSocket server/handler. 返回基础WebSocket服务器/处理程序。You can use 您可以使用wsInstance.getWss().clients
to obtain a list of all the connected WebSocket clients for this server.wsInstance.getWss().clients
客户端来获取此服务器所有连接的WebSocket客户端的列表。
Note that this list will include all clients, not just those for a specific route - this means that it's often not a good idea to use this for broadcasts, for example.请注意,该列表将包括所有客户端,而不仅仅是特定路由的客户端——这意味着,例如,将其用于广播通常不是一个好主意。
Sets up 在给定的express-ws
on the given router
(or other Router-like object). router
(或其他类似路由器的对象)上设置express-ws
。You will only need this in two scenarios:您只需要在两种情况下使用:
options.leaveRouterUntouched
, oroptions.leaveRouterUntouched
,或In most cases, you won't need this at all.在大多数情况下,你根本不需要这个。
This module is written in ES6 and uses ESM.该模块使用ES6编写,并使用ESM。