本文介绍了使用socket.io和空消息队列发送消息到特定客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经用socket.io疯了!文档是如此糟糕,这根本不是真的。
我想通过socket.io向特定客户端发送反馈
我的服务器端如下所示:
app.get('/ upload',requiresLogin,function请求,响应){
response.render('upload / index.jade');
io.sockets.on('connection',function(socket){
console.log(' SOCKET ID'+ socket.id);
io.sockets.socket(socket.id).emit('new','hello');
});
});
客户端如下所示:
$(document).ready(function(){
var socket = io.connect('http:// localhost:80 / socket.io / socket.io。 js');
socket.on('new',function(data){
console.log(socket.id);
console.log(data);
/ /$('#state').html(data.status);
});
});
但客户端根本没有任何意义。我几乎尝试了一切。有人可以告诉我我在做错了吗:(
解决方案
向特定客户端发送消息,保存连接到对象中的服务器的每个。
var socketio = require('socket.io');
var clients = {};
var io = socketio.listen(app);
io.sockets.on('connection',function(socket){
clients [socket.id] = socket;
});
然后你可以稍后这样做:
var socket = clients [sId];
socket.emit('show',{});
I´m going crazy with socket.io!Documentation is so bad it's simply not true.
I want to send a feedback to specific client over socket.io
My server side looks like this:
app.get('/upload', requiresLogin, function(request, response) {
response.render('upload/index.jade');
io.sockets.on('connection', function (socket) {
console.log('SOCKET ID ' + socket.id);
io.sockets.socket(socket.id).emit('new', 'hello');
});
});
and the client side looks like this:
$(document).ready(function() {
var socket = io.connect('http://localhost:80/socket.io/socket.io.js');
socket.on('new', function (data) {
console.log(socket.id);
console.log(data);
//$('#state').html(data.status);
});
});
but the client does simply nothing. I have tried nearly everything. Can someone tell me what I am doing wrong, please! :(
解决方案
to send a message to a specific client save every one that connects to the server in an Object.
var socketio = require('socket.io');
var clients = {};
var io = socketio.listen(app);
io.sockets.on('connection', function (socket) {
clients[socket.id] = socket;
});
then you can later do something like this:
var socket = clients[sId];
socket.emit('show', {});
这篇关于使用socket.io和空消息队列发送消息到特定客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!