2
- 「socket.emit」不發送消息到發送它的客戶端,而
- 「socket.broadcast.emit」將消息發送到所有的客戶端包括髮件人。
我想不通我在這裏失蹤。 對於第二種情況,我檢查socket.id在發送該消息,並記錄它被接收到的響應的情況下,它被證明是相同的socket.emit不發射消息給發件人
socket.js
var socket = require('socket.io')(),
socketApi = {};
socketApi.io = socket;
socketApi.io.on('connection',(client) => {
client.on('clientMessage', (msg) => {
console.log('hi');
client.emit('serverMessage',msg);
})
client.on('disconnect',() => {
socketApi.io.emit('serverMessage','client is disconnected');
console.log('disconnected');
})
})
module.exports = socketApi;
client.js
getServerResponse() {
socket.on('serverMessage',(msg) => {
console.log(msg);
})
}
- 你叫'getServerResponse」功能? –
是的...我使用SocketAPI.io.emit或client.broadcast.emit時得到響應 – sanket
'client.on('clientMessage',(msg)=> {;'在這裏做什麼分號? –