我有一个Express.io
的问题:我试图创建一个聊天,但我不能使用Broadcast
方法。
没有错误信息,但是什么也没发生。
app.js
var express = require('express.io')
, index = require('./routes/index.js')
, http = require('http')
, path = require('path');
var app = express();
app.configure(function(){
//configure options
});
app.http().io();
app.get('/', index.index);
app.io.route('ready', function(req) {
req.io.broadcast('newUser');
});
app.listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
user.js
io = io.connect();
io.emit('ready');
io.on('newUser', function(data) {
console.log("New user !!");
});
错误2 WebSocket connection to 'ws://tchat.aws.af.cm/socket.io/1/websocket/n8Jm9Q7YYL8YdPRN4dxU' failed: Unexpected response code: 502
req.io.broadcast
向所有连接的客户端广播,除了与请求相关联的客户端。您应该使用app.io.broadcast
向所有连接的客户端广播。
参见给出的示例:https://github.com/techpines/express.io/tree/master/examples/broadcasting