如何从节点实现套接字客户端



我有一个套接字实现,它可以很好地与在nodejs中运行的套接字服务器和在浏览器中使用vue运行的套接字客户端配合使用。

问题是,我试图从另一个node.js项目实现一个新的客户端,但没有成功。

我遗漏了什么吗?请帮忙!

我已经关注了这个链接,但仍然无法使其工作。https://github.com/socketio/socket.io-client

Server.js

const Socketio = require('socket.io')(Http)
Socketio.on('connection', (socket) => {
socket.on('join_room', function (room) {
console.log('join room -> ', room)
socket.join(room)
})
}
Http.listen(3000, () => {
console.log('sockets listening at : 3000')
})

客户端-浏览器-Vue

created() {
this.socket = io('http://localhost:3000')
this.socket.on('connect', () => {
console.log("its connected")
this.socket.emit('join_room', this.id)
})
}

节点Js客户端

var io = require('socket.io-client');
var socket = io.connect('http://localhost:3000', { reconnect: true });

socket.on('connect', () => {
console.log("it never logs here, and it doesn't emit join room either");
socket.emit('join_room', id)
});

尝试先连接:

var io = require('socket.io-client');
var socket = io.connect('http://localhost:3000', { reconnect: true }); // here you forget to make a connexion 
socket.on('connect', () => {
console.log("it never logs here, and it doesn't emit join room either");
socket.emit('join_room', id)
});

最新更新