# 3.1.7 初始化示例-额外Headers

这仅在启用了`轮询`传输（默认情况下）时才有效。将`WebSocket`用作传输时，不会附加自定义头。这是因为WebSocket握手不支持自定义头文件。（见[WebSocket协议RFC](https://tools.ietf.org/html/rfc6455#section-4)）

客户端：

```javascript
const socket=io({
    transportOptions:{
        polling:{
            extraHeaders:{
                "x-clientId":"abc"
            }
        }
    }
})
```

服务端

```javascript
const io= require('socket.io')()

// 中间器件

io.use((socket,next)=>{
    const clientId= socket.handshake.headers['x-clientId'];
    if (isValid(clientId)){
        return next()
    }
    return next(new Error("authentication error"))
})
```


---

# 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/3-ke-hu-duan-apiclientapi/3.1-io/initialization_examples_with_extraheaders.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.
