I´m going crazy with socket.io! Documentation is so bad it's simply not true.
我和socket . io´m疯了!文档是如此糟糕以至于根本不是真的。
I want to send a feedback to specific client over socket.io
我想通过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! :(
但是客户什么都不做。我几乎什么都试过了。有人能告诉我我做错了什么吗?:(
9 个解决方案
#1
53
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', {});
#2
18
A couple of ways to send feedback to a specific client over socket.io include:
通过套接字向特定客户端发送反馈的两种方法。输入输出包括:
-
As stated by pkyeck, save all clients to an Object, so you can send to these specific clients later in your route handlers, e.g.:
如pkyeck所述,将所有客户端保存到一个对象中,以便稍后在路由处理程序中将其发送到这些特定的客户端,例如:
var sessionsConnections = {}; sio.on('connection', function (socket) { // do all the session stuff sessionsConnections[socket.handshake.sessionID] = socket; });
-
or, use socket.io's built in support for rooms - subscribe each client to a room on connection and send via this room within route handlers, e.g.:
或者,使用套接字。io内置了对房间的支持——订阅每个客户端到一个连接的房间,并通过这个房间发送路由处理程序,例如:
sio.on('connection', function (socket) { // do all the session stuff socket.join(socket.handshake.sessionID); // socket.io will leave the room upon disconnect }); app.get('/', function (req, res) { sio.sockets.in(req.sessionID).send('Man, good to see you back!'); });
Acknowledgement:
确认:
http://www.danielbaulig.de/socket-ioexpress/#comment-1158
http://www.danielbaulig.de/socket-ioexpress/评论- 1158
Note that both these example solutions assume that socket.io and express have been configured to use the same session store and hence refer to the same session objects. See the links above and below for more details on achieving this:
注意,这两个示例解决方案都假设了这个套接字。io和express被配置为使用相同的会话存储,因此引用相同的会话对象。有关实现这一目标的更多细节,请参阅上面和下面的链接:
https://github.com/LearnBoost/socket.io/wiki/Authorizing
https://github.com/LearnBoost/socket.io/wiki/Authorizing
#3
7
2 things
两件事
1) You should really place your io.sockets.on(..)
outside your app/update
route to prevent adding multiple listeners for clients.
1)你应该把你的io.s .on(.. .)放在你的应用/更新路径之外,以防止为客户添加多个监听器。
2) io.sockets.socket(id); should not be used, it should have been socket.emit('new', 'hello')
2)io.sockets.socket(id);不应该使用,应该是插座。发出(“新”、“你好”)
#4
5
In socket.io 1.0, this is how it would work. It may work for lower versions, but I cannot guarantee it.
在套接字。io 1.0,这就是它的工作原理。它可能适用于低版本,但我不能保证。
socket.to(socket_id_here).emit('new', 'hello');
This works because socket.io automatically adds a socket to a room with the socket's id on connection.
这个工作因为插座。io自动将一个套接字添加到连接有套接字id的房间。
Also, if you plan to upgrade to version 1.0, there are a lot of changes to the api, so you'll sometimes have to change some code to make it work in 1.0.
此外,如果您计划升级到1.0版本,api会有很多变化,所以您有时需要修改一些代码才能使它在1.0版本中工作。
#5
4
The correct way to do this in Socket.io 1.0+ is:
正确的方法是在插座中这样做。io 1.0 +是:
io.to(users_socket_id).emit('new', 'hello');
You can also substitute a room name for 'users_socket_id' to emit to a specific socket.io room.
您还可以将“users_socket_id”的房间名称替换为特定的套接字。io的房间。
#6
2
First of all, you cannot use socket.id
on client side.
首先,您不能使用套接字。在客户端id。
And then change the line
然后改变直线
var socket = io.connect('http://localhost:80/socket.io/socket.io.js');
var插座= io.connect(http://localhost:80 / socket . io / socket.io.js);
to
来
var socket = io.connect('http://localhost:80/');
var插座= io.connect(http://localhost:80 /);
#7
0
I believe io.sockets.socket has been removed and has been a problem in Socket.IO (https://github.com/socketio/socket.io/issues/1618).
我相信io.sockets。套接字已经被移除,并且已经成为套接字中的一个问题。IO(https://github.com/socketio/socket.io/issues/1618)。
You can use io.sockets.connected[socket.id] and store the socket.id to reference with the user via username:
您可以使用io.sockets.connected[插座。并存储套接字。id以用户名引用用户:
var usernames = {};
usernames[username] = socket.id;
// Sending the message
io.sockets.connected[usernames[username]].emit(...);
I don't see it anywhere on the documentation so I can see how this hasn't been answered. Also, if you don't have any reference via usernames, you can instead try:
我没有在文档中看到它,所以我可以看到它是如何没有被回答的。此外,如果你没有任何用户名的参考资料,你可以试试:
users[socket.id] = socket.id;
to duplicate the key and value for easy access.
复制键和值以方便访问。
There is also possibly another way by using io.clients as described below, but I haven't used that method.
还有一种使用io的方法。下面描述的客户端,但是我还没有使用这种方法。
OTHER SOLUTION: Send message to specific client with socket.io and node.js
其他解决方案:使用套接字向特定客户端发送消息。io和node . js
#8
0
Have you tried using?
你试着用吗?
var socket = io.connect('http://localhost:80/');
#9
0
i tired with the latest version of node and socket.io below i am going to post complete code
我厌倦了最新版本的node和socket。下面我将发布完整的代码
<ul id="messages"></ul>
<form action="">
<input id="id1" /><button>Send</button>
</form>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
var io = io.connect();
$('form').submit(function(){
io.emit('drumevent', $('#id1').val());
$('#id1').val('');
return false;
});
io.on('drumevent', function(msg){
console.log(msg);
$('#messages').append($('<li></li>').text(msg.message+' quantity = '+msg.quantity));
});
</script>
server side code
服务器端代码
var usernames = {};io.on('connection', function(socket){usernames["username"] = socket.id;
socket.on('drumevent', function(msg){
var socketId = socket.id;io.to(socketId).emit('drumevent', data);
#1
53
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', {});
#2
18
A couple of ways to send feedback to a specific client over socket.io include:
通过套接字向特定客户端发送反馈的两种方法。输入输出包括:
-
As stated by pkyeck, save all clients to an Object, so you can send to these specific clients later in your route handlers, e.g.:
如pkyeck所述,将所有客户端保存到一个对象中,以便稍后在路由处理程序中将其发送到这些特定的客户端,例如:
var sessionsConnections = {}; sio.on('connection', function (socket) { // do all the session stuff sessionsConnections[socket.handshake.sessionID] = socket; });
-
or, use socket.io's built in support for rooms - subscribe each client to a room on connection and send via this room within route handlers, e.g.:
或者,使用套接字。io内置了对房间的支持——订阅每个客户端到一个连接的房间,并通过这个房间发送路由处理程序,例如:
sio.on('connection', function (socket) { // do all the session stuff socket.join(socket.handshake.sessionID); // socket.io will leave the room upon disconnect }); app.get('/', function (req, res) { sio.sockets.in(req.sessionID).send('Man, good to see you back!'); });
Acknowledgement:
确认:
http://www.danielbaulig.de/socket-ioexpress/#comment-1158
http://www.danielbaulig.de/socket-ioexpress/评论- 1158
Note that both these example solutions assume that socket.io and express have been configured to use the same session store and hence refer to the same session objects. See the links above and below for more details on achieving this:
注意,这两个示例解决方案都假设了这个套接字。io和express被配置为使用相同的会话存储,因此引用相同的会话对象。有关实现这一目标的更多细节,请参阅上面和下面的链接:
https://github.com/LearnBoost/socket.io/wiki/Authorizing
https://github.com/LearnBoost/socket.io/wiki/Authorizing
#3
7
2 things
两件事
1) You should really place your io.sockets.on(..)
outside your app/update
route to prevent adding multiple listeners for clients.
1)你应该把你的io.s .on(.. .)放在你的应用/更新路径之外,以防止为客户添加多个监听器。
2) io.sockets.socket(id); should not be used, it should have been socket.emit('new', 'hello')
2)io.sockets.socket(id);不应该使用,应该是插座。发出(“新”、“你好”)
#4
5
In socket.io 1.0, this is how it would work. It may work for lower versions, but I cannot guarantee it.
在套接字。io 1.0,这就是它的工作原理。它可能适用于低版本,但我不能保证。
socket.to(socket_id_here).emit('new', 'hello');
This works because socket.io automatically adds a socket to a room with the socket's id on connection.
这个工作因为插座。io自动将一个套接字添加到连接有套接字id的房间。
Also, if you plan to upgrade to version 1.0, there are a lot of changes to the api, so you'll sometimes have to change some code to make it work in 1.0.
此外,如果您计划升级到1.0版本,api会有很多变化,所以您有时需要修改一些代码才能使它在1.0版本中工作。
#5
4
The correct way to do this in Socket.io 1.0+ is:
正确的方法是在插座中这样做。io 1.0 +是:
io.to(users_socket_id).emit('new', 'hello');
You can also substitute a room name for 'users_socket_id' to emit to a specific socket.io room.
您还可以将“users_socket_id”的房间名称替换为特定的套接字。io的房间。
#6
2
First of all, you cannot use socket.id
on client side.
首先,您不能使用套接字。在客户端id。
And then change the line
然后改变直线
var socket = io.connect('http://localhost:80/socket.io/socket.io.js');
var插座= io.connect(http://localhost:80 / socket . io / socket.io.js);
to
来
var socket = io.connect('http://localhost:80/');
var插座= io.connect(http://localhost:80 /);
#7
0
I believe io.sockets.socket has been removed and has been a problem in Socket.IO (https://github.com/socketio/socket.io/issues/1618).
我相信io.sockets。套接字已经被移除,并且已经成为套接字中的一个问题。IO(https://github.com/socketio/socket.io/issues/1618)。
You can use io.sockets.connected[socket.id] and store the socket.id to reference with the user via username:
您可以使用io.sockets.connected[插座。并存储套接字。id以用户名引用用户:
var usernames = {};
usernames[username] = socket.id;
// Sending the message
io.sockets.connected[usernames[username]].emit(...);
I don't see it anywhere on the documentation so I can see how this hasn't been answered. Also, if you don't have any reference via usernames, you can instead try:
我没有在文档中看到它,所以我可以看到它是如何没有被回答的。此外,如果你没有任何用户名的参考资料,你可以试试:
users[socket.id] = socket.id;
to duplicate the key and value for easy access.
复制键和值以方便访问。
There is also possibly another way by using io.clients as described below, but I haven't used that method.
还有一种使用io的方法。下面描述的客户端,但是我还没有使用这种方法。
OTHER SOLUTION: Send message to specific client with socket.io and node.js
其他解决方案:使用套接字向特定客户端发送消息。io和node . js
#8
0
Have you tried using?
你试着用吗?
var socket = io.connect('http://localhost:80/');
#9
0
i tired with the latest version of node and socket.io below i am going to post complete code
我厌倦了最新版本的node和socket。下面我将发布完整的代码
<ul id="messages"></ul>
<form action="">
<input id="id1" /><button>Send</button>
</form>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
var io = io.connect();
$('form').submit(function(){
io.emit('drumevent', $('#id1').val());
$('#id1').val('');
return false;
});
io.on('drumevent', function(msg){
console.log(msg);
$('#messages').append($('<li></li>').text(msg.message+' quantity = '+msg.quantity));
});
</script>
server side code
服务器端代码
var usernames = {};io.on('connection', function(socket){usernames["username"] = socket.id;
socket.on('drumevent', function(msg){
var socketId = socket.id;io.to(socketId).emit('drumevent', data);