socket.emit(eventName[, ...args][, ack])

优质
小牛编辑
128浏览
2023-12-01
  • eventName (字符串)
  • args
  • ack (Function)
  • Returns Socket

通过提供的name时间名称向socket标志发送事件,任何其他的参数都可被包含,所有可被序列化的数据结构都支持,包括Buffer。

socket.emit('hello', 'world');
socket.emit('with-binary', 1, '2', { 3: '4', 5: new Buffer(6) });

ack参数将用来响应服务器用来确认消息的应答。

socket.emit('ferret', 'tobi', (data) => {
  console.log(data); // data will be 'woot'
});

// server:
//  io.on('connection', (socket) => {
//    socket.on('ferret', (name, fn) => {
//      fn('woot');
//    });
//  });