I have the Express code in server JS file:
我在服务器JS文件中有Express代码:
app.post('/confirm', function (req, res) {
// Here I need to send socket with emit()
});
Below mentioned code I have Socket.io:
下面提到的代码我有Socket.io:
io.on('connection', function (client) {
// All methods
});
Problem is that I can not get access to socket
in express method app.post()
and can not send data by POST action.
问题是我无法在快速方法app.post()中访问套接字,并且无法通过POST操作发送数据。
How can I use that?
我该怎么用?
1 个解决方案
#1
0
You can emit data to specific connected sockets using the following:
您可以使用以下方法将数据发送到特定连接的套接字:
io.to(socket_id).emit('something', {"bar":"foo"});
The "socket_id" variable is, as probably guessed, the socket.id from the connected socket.
正如可能猜到的那样,“socket_id”变量来自连接套接字的socket.id.
You will probably have to store them along with some other identification in an array or object to send data to the correct clients later using express routes.
您可能必须将它们与数组或对象中的其他标识一起存储,以便稍后使用快速路由将数据发送到正确的客户端。
PS: As your code is
PS:正如你的代码那样
io.on('connection', function (client) {
// All methods
});
you would use client.id
to get the socket id.
你会使用client.id来获取套接字ID。
#1
0
You can emit data to specific connected sockets using the following:
您可以使用以下方法将数据发送到特定连接的套接字:
io.to(socket_id).emit('something', {"bar":"foo"});
The "socket_id" variable is, as probably guessed, the socket.id from the connected socket.
正如可能猜到的那样,“socket_id”变量来自连接套接字的socket.id.
You will probably have to store them along with some other identification in an array or object to send data to the correct clients later using express routes.
您可能必须将它们与数组或对象中的其他标识一起存储,以便稍后使用快速路由将数据发送到正确的客户端。
PS: As your code is
PS:正如你的代码那样
io.on('connection', function (client) {
// All methods
});
you would use client.id
to get the socket id.
你会使用client.id来获取套接字ID。