my.onSocketMessage

Use this API to listen to the event of receiving server messages by WebSocket.

Supported types of mini programs: DSL

Callback Return Value

Property

Type

Description

data

String / ArrayBuffer

The messages returned by the server, in text string or base64 encoded string. 

isBuffer

Boolean

Whether the message is base64-encoded.

  • If the value of this field is true, the data field represents the received base64-encoded binary strings.
  • If the value of this field is empty, the data field represents normal strings.

Sample Code

copy
my.connectSocket({
  url: 'Server URL'
})

my.onSocketMessage(function(res) {
  console.log('Server content received:' + res.data)
})

Note: The case is only for reference. Please use your own URL to test.