我正在尝试通过Socket.io向客户端发送数据。 如果建立连接,我将收到一条消息。但是在socket.on('connection')之外,客户端没有得到任何东西。
import io from 'socket.io-client'
const socket = io('http://192.168.178.54:3000', {
transports: ['websocket', 'polling'],
})
socket.on('connection', data => socket.emit('recruiter_id', recruiter_id)) // TOP
socket.on('update_contact', data => console.log) // FLOP
socket.on('connection', data => console.log) // FLOP
socket.on('haha', data => console.log) // FLOP
this.io = require('socket.io')(this.server, {transports : ['polling', 'websocket']});
this.io.on('connect', (socket) => {
console.log('connected');
socket.emit('update_contact', {bla: "hi"});
socket.emit('connection', {'michael' : 'ist doof'});
socket.emit('haha', "hi"); // This will be emitted
socket.on('recruiter_id', (id) => {
this.socketMap.set(id, id);
socket.emit('update_contact', {bla: "hi"});
})
socket.emit('update_contact', {bla: "hi"});
});
服务器正在另一台计算机上运行。
答案 0 :(得分:0)
您的客户端不会收听connection
,因为箭头功能永远不会执行。您应该听取connect
,而不是connected
。试试这个:
CLIENT.JS
socket.on('connect', () => console.log('client connected'));
socket.on('connection', (data) => console.log('connection:', data));
答案 1 :(得分:0)
Removing transports from client and server was the solution.