跳到主要内容

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)

参数类型说明
contentstring消息内容
conversationIdinteger会话 ID
typeinteger消息类型:1=文字,2=图片,3=视频,4=文件
imageUrlstring图片地址(type=2 时必传)
imageThumbUrlstring图片缩略图地址
videoUrlstring视频地址(type=3 时必传)
fileUrlstring文件地址(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)

参数类型说明
idinteger消息 ID
contentstring消息内容
conversationIdinteger会话 ID
typeinteger消息内容类型,1文字 2图片 3视频 4文件
imageUrlstring图片地址
imageThumbUrlstring图片缩略图地址
videoUrlstring视频地址
fileUrlstring文件地址
createTimestring发送时间
selfboolean是否为自己发送的消息
fromUserIdstring发送者 ID
fromNickNamestring发送者昵称

心跳检测

每 5 秒发送一次心跳,超过 60 秒未收到心跳服务端将断开连接。

请求

{"method":"PING"}

响应

{
"success": true,
"method": "PING",
"msg": "success",
"data": "PONG"
}