2016-11-01 65 views
4

我有一個快速路由器爲express-ws npm包定義。路由器看起來是出口,如:包express-ws不讓客戶端連接

//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路由。我在這裏錯過了什麼嗎?有沒有更好的方法來創建在Express 4上運行的websocket?

這是我在客戶端看到的錯誤!我究竟做錯了什麼?

enter image description here

回答

2

修改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}; 

變化斌/ WWW

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

這應該被接受。 'app,server'部分。 –