WebSocket 聊天
建立连接
连接地址: wss://wbs.p2p.mexc.com/ws
接口权限要求: P2P 下单、P2P 广告主
请实时监听连接状态,连接断开后请重新连接。
连接参数
| 参数 | 必传 | 说明 |
|---|---|---|
| listenKey | 是 | 通过生成 listenKey 接口生成 |
| conversationId | 是 | 通过获取聊天会话信息接口获取 |
连接示例
wss://wbs.p2p.mexc.com/ws?listenKey=pqia91ma19a5s61cv6a81va65sd099v8a65a1&conversationId=123
发送消息
请求
{
"method": "SEND_MESSAGE",
"params": "{\"content\":\"这是设计稿截图\",\"conversationId\":50012,\"type\":2,\"imageUrl\":\"https://cdn.example.com/images/design_v3.png\",\"imageThumbUrl\":\"https://cdn.example.com/images/design_v3_thumb.jpg\",\"videoUrl\":\"\",\"fileUrl\":\"\"}"
}
消息体字段(params 中的 JSON)
| 参数 | 类型 | 说明 |
|---|---|---|
| content | string | 消息内容 |
| conversationId | integer | 会话 ID |
| type | integer | 消息类型:1=文字,2=图片,3=视频,4=文件 |
| imageUrl | string | 图片地址(type=2 时必传) |
| imageThumbUrl | string | 图片缩略图地址 |
| videoUrl | string | 视频地址(type=3 时必传) |
| fileUrl | string | 文件地址(type=4 时必传) |
响应
{
"success": true,
"method": "SEND_MESSAGE",
"msg": "success"
}
接收消息
服务端主动推送消息到客户端:
{
"success": true,
"msg": "success",
"method": "RECEIVE_MESSAGE",
"data": "{\"id\":1,\"content\":\"hello\",\"conversationId\":50012,\"type\":1,\"createTime\":\"2025-01-26 12:00:00\",\"self\":false,\"fromUserId\":\"abc123\",\"fromNickName\":\"对手方\"}"
}
消息体字段(data 中的 JSON)
| 参数 | 类型 | 说明 |
|---|---|---|
| id | integer | 消息 ID |
| content | string | 消息内容 |
| conversationId | integer | 会话 ID |
| type | integer | 消息内容类型,1文字 2图片 3视频 4文件 |
| imageUrl | string | 图片地址 |
| imageThumbUrl | string | 图片缩略图地址 |
| videoUrl | string | 视频地址 |
| fileUrl | string | 文件地址 |
| createTime | string | 发送时间 |
| self | boolean | 是否为自己发送的消息 |
| fromUserId | string | 发送者 ID |
| fromNickName | string | 发送者昵称 |
心跳检测
每 5 秒发送一次心跳,超过 60 秒未收到心跳服务端将断开连接。
请求
{"method":"PING"}
响应
{
"success": true,
"method": "PING",
"msg": "success",
"data": "PONG"
}