[英]NodeJS, Express, Sockets.io: Cannot get .on(“connection”… to fire on server
我正在嘗試將sockets.io集成到我的Express應用程序中。 我使用Express Generator來格式化我的應用程序,因此必須在bin / www中設置Websocket。 我發現了一個類似的問題,並應用了此處找到的答案,指示我在app.js中創建我的套接字對象,將其附加到該應用程序對象,並通過bin / www中的導出對其進行訪問以將其附加到服務器。
這是我當前的app.js文件:
var express = require('express'),
path = require('path'),
favicon = require('serve-favicon'),
bodyParser = require('body-parser'),
sanitizer = require('sanitizer'),
socket_io = require('socket.io'),
config = require('./lib/config'),
db = require('./lib/db'),
fs = require('fs'),
app = express(),
io = socket_io();
app.io = io;
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(bodyParser.json({strict: false}));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
app.use(function(req,res,next){
req.io = io;
next();
});
app.io.sockets.on('connection', function(socket){
console.log('connected!');
});
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.json({
code: err.status,
err: err.message
}).end();
});
module.exports = app;
這是我的bin / www文件,與socket.io和http服務器有關:
var app = require('../app');
var debug = require('debug')('fresco:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Socket.io
*/
app.io.attach(server);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
我正在嘗試訪問通過ws://localhost:3000/
在本地運行的服務器,但是在客戶端上沒有任何響應。 我在Express中實現Websockets時是否做錯了事情,從而破壞了我的websockets服務器? 我無法想象為什么它不起作用,因為在app.js中創建的對象已成功傳遞到bin / www。 將console.log放在bin / www的末尾將顯示以下內容:
{ nsps:
{ '/':
{ name: '/',
server: [Circular],
sockets: [],
connected: {},
fns: [],
ids: 0,
acks: {},
adapter: [Object],
_events: [Object] } },
_path: '/socket.io',
_serveClient: true,
_adapter: [Function: Adapter],
_origins: '*:*',
sockets:
{ name: '/',
server: [Circular],
sockets: [],
connected: {},
fns: [],
ids: 0,
acks: {},
adapter: { nsp: [Circular], rooms: {}, sids: {}, encoder: {} },
_events: { connection: [Function] } },
eio:
{ clients: {},
clientsCount: 0,
pingTimeout: 60000,
pingInterval: 25000,
upgradeTimeout: 10000,
maxHttpBufferSize: 100000000,
transports: [ 'polling', 'websocket' ],
allowUpgrades: true,
allowRequest: [Function],
cookie: 'io',
ws:
{ domain: null,
_events: {},
_maxListeners: undefined,
options: [Object],
path: null,
clients: [] },
_events: { connection: [Function] } },
httpServer:
{ domain: null,
_events:
{ connection: [Function: connectionListener],
clientError: [Function],
close: [Function],
upgrade: [Function],
request: [Function],
error: [Function: onError],
listening: [Function: onListening] },
_maxListeners: undefined,
_connections: 0,
_handle:
{ fd: undefined,
reading: false,
owner: [Circular],
onread: null,
onconnection: [Function: onconnection],
writeQueueSize: 0 },
_usingSlaves: false,
_slaves: [],
allowHalfOpen: true,
pauseOnConnect: false,
httpAllowHalfOpen: false,
timeout: 120000,
_connectionKey: '4:null:3000' },
engine:
{ clients: {},
clientsCount: 0,
pingTimeout: 60000,
pingInterval: 25000,
upgradeTimeout: 10000,
maxHttpBufferSize: 100000000,
transports: [ 'polling', 'websocket' ],
allowUpgrades: true,
allowRequest: [Function],
cookie: 'io',
ws:
{ domain: null,
_events: {},
_maxListeners: undefined,
options: [Object],
path: null,
clients: [] },
_events: { connection: [Function] } } }
嘗試從app.io.sockets.on
app.io = io()
。中刪除套接字。 因此io.sockets不是函數,而io.on('connection', function(){})
是io.on('connection', function(){})
。 看起來像是一個錯字。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.