Package express-ws不允许客户连接

我有一个express-ws npm包定义的快速路由器。 路由器的外观如下所示:

//ws.js var express = require('express'); var router = express.Router(); var connections = {}; router.ws('/connect/', function (ws, req) { // console.log(ws); ws.on('close', function(x, y, z) { console.log(a, b, c); }); }); module.exports = router; 

快速应用中的(不是全部)语句是(并按顺序):

 //app.js var app = express(); var expressWs = require('express-ws')(app); var index = require('./routes/index'); var ws = require('./routes/ws'); ... ... app.use('/ws', ws); app.use('/*', index); 

服务器具有像这样的语句:

 //bin/www - Generated by express generator var app = require('../app'); var debug = require('debug')('server:server'); var http = require('http'); var port = normalizePort(process.env.PORT || '3000'); app.set('port', port); var server = http.createServer(app); server.listen(port); 

运行后,我无法通过websocket连接到/ws路由。 我在这里错过了什么? 有没有更好的方法来创build运行在Express 4上的websocket?

这是我在客户端看到的错误! 我究竟做错了什么?

在这里输入图像描述

       

网上收集的解决方案 "Package express-ws不允许客户连接"

更改为app.js

 var app = express(); var server = require('http').Server(app); var expressWs = require('express-ws')(app,server); ... ... //module.exports = app; module.exports = {app: app,server: server}; 

更改bin / www

 //var app = require('../app'); var app = require('../app').app; ... ... //var server = http.createServer(app); var server = require('../app').server;