有一个代码行:
io.sockets.socket(socketid).emit('counter', { all : data.all, dialog : data.dialog });
它给了我例外:
TypeError: Object #<Namespace> has no method 'socket'
我的连接事件:
io.sockets.on('connection', function(socket) {});
为什么对象套接字不是?
完整代码:
var socketio = require('socket.io');
var server = http.createServer(app).listen(8181, function(){
console.log("Express server listening on port "+ app.get('port') +" in "+ app.get('env') +" mode.");
});
var io = socketio.listen(server);
io.on('connection', function(socket) {});
答案 0 :(得分:3)
与快递一起使用的套接字服务器创建。请查看http://socket.io/docs/以获取更多信息和帮助。
var app = require('express').createServer();
var io = require('socket.io')(app);
app.listen(8181);
io.on('connect', function(socket) {
socket.on('example', function(data) {
//parse the data, do something, create response data object, etc.
});
});
使用HTTP + Express
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen(8181);
io.on('connect', function(socket) {
socket.on('example', function(data) {
//parse the data, do something, create response data object, etc.
});
});
对于您尝试创建的发射(我的下面示例将仅通过提供.to(socket.id)广播到连接的套接字)
io.to(socket.id).emit('counter', {all : data.all, dialog : data.dialog});