# UDPSocket.onMessage(function listener)

监听收到消息的事件。

# 支持情况

项 说明
兼容性 兼容微信

# 语法

udpSocket.onMessage(function listener)

# 参数

function listener

收到消息时执行的回调函数。

参数 类型 说明
message ArrayBuffer 收到的数据
remoteInfo Object 发送方信息
remoteInfo.address string 发送方地址
remoteInfo.port number 发送方端口
remoteInfo.size number 消息大小,单位字节
localInfo Object 接收方(本机)信息
localInfo.address string 本机地址
localInfo.port number 本机已绑定的端口

# 返回值

void。

# 示例代码

udpSocket.onMessage((res) => {
  console.log('收到数据,来自:', res.remoteInfo.address, ':', res.remoteInfo.port)
  console.log('数据:', res.message)
})

# 与微信差异

无差异(行为与微信小游戏一致)。

上次更新: 8/10/2026, 5:36:37 PM