# 2.6 emit 备忘单

```javascript
io.on('connect',onConnect)

function onConnect(){
    // 发消息到客户端
    socket.emit('hello',"can you hear me?",1,2,"abc");

    // 发送到是所有客户端除了发送者
    socket.broadcast.emit("boradcast","hello friends!");

    // 发送到 “game” room中除发件人以外的所有客户端
    socket.to("game").emit("nice game","let't play a game");

    // 发送到“game1”和/或“game2”房间中的所有客户端，发件人除外
    socket.to('game1').to('game2').emit("nice game","let's play a game (too)");

    // 发送给“游戏”室中的所有客户，包括发件人
    io.in('game').emit('big-announcement',"the game will start soon");

    // 发送到命名空间“mynamespace”中的所有客户端，包括发件人
    io.of('myNamespace').emit('bigger-announcement',"the tournament will start soon");

    // 发送到特定命名空间中的特定房间，包括发件人
    io.of('myNamespace').to('root').emit('event',"message");

    // 警告：`socket.to(socket.id).emit()` ,不会工作，因为它会发送给房间里的每个人
    // 命名 `socket.id` ，但为发件人。请改用经典的“socket.emit（）”。

    // 带确认发送
    socket.emit('question',"do you think so?",(answer)=>{

    })

    // 不压缩发送
    socket.compress(false).emit('uncomressed',"that's rough");

    // 指定要发送的数据是否具有二进制数据
    socket.binary(false).emit('what',"I hava no binaries!");

    // 发送到此节点上的所有客户端（使用多个节点时
    io.local.emit('hi',"my lovely babies");

    // 发送到所有连接的客户端
    io.emit('an event send to all connected clients';)

}
```

**注意**：以下事件是保留的，应用程序不应将其用作事件名称：

* `erorr`
* `connect`
* `disconnect`
* `disconnecting`&#x20;
* `newListener`
* `removeListener`
* `ping`
* `pong`


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://socket.gitbook.io/docs/2-wen-dang-docs/emit_cheatsheet.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
