const socket = require('socket.io-client')('http://192.168.0.8:5000', {
reconnection: true,
reconnectionDelay: 10000
});
socket.on('connect', (data) => {
console.log('Connected to Socket');
});
socket.on('event_name', (data) => {
console.log("-----------------received event data from the socket io server");
});
//either 'io server disconnect' or 'io client disconnect'
socket.on('disconnect', (reason) => {
console.log("client disconnected");
if (reason === 'io server disconnect') {
// the disconnection was initiated by the server, you need to reconnect manually
console.log("server disconnected the client, trying to reconnect");
socket.connect();
}else{
console.log("trying to reconnect again with server");
}
// else the socket will automatically try to reconnect
});
socket.on('error', (error) => {
console.log(error);
});
7条答案
按热度按时间i86rm4rw1#
这应该可以使用Socket.IO-client:https://github.com/LearnBoost/socket.io-client
ztmd8pv52#
添加前面给出的解决方案的示例。通过使用
socket.io-client
https://github.com/socketio/socket.io-client客户端:
服务器端:
运行:
打开2个控制台并运行
node server.js
和node client.js
ff29svar3#
安装套接字.io-client后:
客户端代码如下所示:
谢谢。
kknvjkwl4#
pu82cl6c5#
是的,你可以使用任何客户端,只要它是由socket.io支持.无论是它的节点,java,android或swift.所有你需要做的是安装socket.io的客户端软件包.
sshcrbum6#
客户端代码:我有一个要求,我的nodejs网络服务器应该作为服务器以及客户端工作,所以我添加了下面的代码时,我需要它作为客户端,它应该工作正常,我正在使用它,并为我工作正常!!!
pbgvytdp7#
像这样的东西对我很有效