> For the complete documentation index, see [llms.txt](https://socket.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://socket.gitbook.io/docs/3-ke-hu-duan-apiclientapi/3.1-io/initialization_examples_with_extraheaders.md).

# 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"))
})
```
