This would work without the .once call.
如果没有。once调用,这就可以工作了。
I am trying to send a single message from server side code with
我正在尝试从服务器端代码发送一条消息
io.on('connect', function (socket) { socket.once('connect', function() { socket.emit('mydata', { feed: result});
io。on('connect', function(套接字){插座)。一次(“连接”功能(){插座。发出(mydata,{提要:结果});
but I get nothing on the client side unless I remove the second line. I also tried socket.once('mydata', { feed: result}) but:
但是我在客户端什么都得不到除非我删除了第二行。我也试过插座。一次(“mydata”,{feed: result}),但是:
throw new TypeError('listener must be a function');
In other words how can one send a message to the client side only when there is new 'mydata'?
换句话说,只有当有新的“mydata”时,才能向客户端发送消息?
Thanks
谢谢
2 个解决方案
#1
3
First off, .once()
is for event listeners only - when you only want to be notified of the next time an event occurs, not subsequent times it occurs. It has nothing to do with sending messages.
首先,.once()仅用于事件监听器——当您只想要下次事件发生时得到通知,而不是后续事件发生时。它与发送消息无关。
What your specific code is trying to do doesn't really make sense. You're in a connect
event and you're trying to listen for another connect
event? It would help if you described more completely what you're trying to accomplish? What does "only when there is a new mydata mean"? How does one tell if there's a new mydata?
您的特定代码要做的事情实际上没有意义。你在一个连接事件中,你试图监听另一个连接事件?如果你能更完整地描述你想要完成的事情,会有帮助吗?“只有当有一个新的mydata时”是什么意思?如何判断是否有新的mydata?
If you're just trying to send the mydata
message, when the client first connects, you can just do this:
如果你只是想发送mydata消息,当客户端第一次连接时,你可以这样做:
io.on('connect', function(socket) {
socket.emit('mydata', { feed: result });
}
If you're trying to make sure you only send this message once to the connection even in case of a reconnect on the same socket, you can do this:
如果您试图确保即使在同一套接字上重新连接,也只向连接发送一次此消息,您可以这样做:
io.on('connect', function(socket) {
if (!socket.sentMydata) {
socket.emit('mydata', { feed: result });
socket.sentMydata = true;
}
}
If you want to make sure this client is ever only sent this message once, even if the page is reloaded or the client navigated away and back, then you will have to use a persistent marker like a cookie or some session id or something like that.
如果您希望确保此客户端只发送此消息一次,即使页面重新加载或客户端来回导航,那么您将必须使用一个持久标记,如cookie或某个会话id或类似的东西。
#2
0
I'm not sure if I understood your question exactly, but you can use mapping.
我不确定我是否理解你的问题,但是你可以使用映射。
var sentHolder = {}
socket.on("mydata", function(mydata){
if(sentHolder[socket.id] == false) {
//sent data to client with emit, this will run only once
sentHolder[socket.id] = true
}
})
#1
3
First off, .once()
is for event listeners only - when you only want to be notified of the next time an event occurs, not subsequent times it occurs. It has nothing to do with sending messages.
首先,.once()仅用于事件监听器——当您只想要下次事件发生时得到通知,而不是后续事件发生时。它与发送消息无关。
What your specific code is trying to do doesn't really make sense. You're in a connect
event and you're trying to listen for another connect
event? It would help if you described more completely what you're trying to accomplish? What does "only when there is a new mydata mean"? How does one tell if there's a new mydata?
您的特定代码要做的事情实际上没有意义。你在一个连接事件中,你试图监听另一个连接事件?如果你能更完整地描述你想要完成的事情,会有帮助吗?“只有当有一个新的mydata时”是什么意思?如何判断是否有新的mydata?
If you're just trying to send the mydata
message, when the client first connects, you can just do this:
如果你只是想发送mydata消息,当客户端第一次连接时,你可以这样做:
io.on('connect', function(socket) {
socket.emit('mydata', { feed: result });
}
If you're trying to make sure you only send this message once to the connection even in case of a reconnect on the same socket, you can do this:
如果您试图确保即使在同一套接字上重新连接,也只向连接发送一次此消息,您可以这样做:
io.on('connect', function(socket) {
if (!socket.sentMydata) {
socket.emit('mydata', { feed: result });
socket.sentMydata = true;
}
}
If you want to make sure this client is ever only sent this message once, even if the page is reloaded or the client navigated away and back, then you will have to use a persistent marker like a cookie or some session id or something like that.
如果您希望确保此客户端只发送此消息一次,即使页面重新加载或客户端来回导航,那么您将必须使用一个持久标记,如cookie或某个会话id或类似的东西。
#2
0
I'm not sure if I understood your question exactly, but you can use mapping.
我不确定我是否理解你的问题,但是你可以使用映射。
var sentHolder = {}
socket.on("mydata", function(mydata){
if(sentHolder[socket.id] == false) {
//sent data to client with emit, this will run only once
sentHolder[socket.id] = true
}
})