# 4.3.18 socket\_to\_room

* `room` (String)
* **Returns** 连接的socekt

为后续事件设置一个修饰符，该事件将仅广播给已加入给定`房间`的客户端（socket本身被排除）。要发送到多个房间，可以多次调用。

要发送到多个房间，可以多次调用。

```javascript
io.on('connection',socket=>{
    // 到一个房间
    socket.to('others').emit('an event',{some:"data"});

    // 到多个房间
    socket.to('room1').to('room2').emit('hello');

    // 发送私信到另外一个socket
    socket.to(/*另外一个socket id*/).emit('hey');

    // 警告：` socket.to（socket.id）.emit（）`将不起作用，因为它将发送给房间中的每个人
    // 名为“socket.id”，但为发件人。请改用经典的“socket.emit（）”。
})
```

**注意**：广播时不支持确认。


---

# 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/4-fu-wu-duan-apiserverapi/4.3-socket/socket_to_room.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.
