我正在尝试将sockets.io集成到我的快递应用程序中 . 我使用快速生成器来格式化我的应用程序,所以我必须在bin / www中设置我的websocket . 我找到了一个类似的问题,并应用了找到的答案here,指示我在app.js中创建我的套接字对象,将其附加到app对象,并通过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/
访问本地运行的服务器,但我在客户端没有得到任何响应 . 我是否在我的websockets服务器中快速实现了websocket中的websockets?我可以't imagine why this isn' t工作,因为在app.js中创建的对象正在成功传递到bin / www . 在bin / www的末尾放置console.log会显示以下内容:
{ 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] } } }
1 回答
尝试从您制作
app.io = io()
的app.io.sockets.on
中删除套接字 . 所以io.sockets不是函数,而是io.on('connection', function(){})
. 看起来像是一个错字 .