Skip to main content

bn.createUDPSocket

createUDPSocket(): undefined | UDPSocket

description Create a UDP Socket Instance

remark supported jssdk >= 4.11.0

example

const udpSocket = bn.createUDPSocket()
udpSocket.onMessage((res) => {
console.log(res.message)
})
udpSocket.send({
address: '',
port: 1234,
message: 'hello'
})

Returns

undefined | UDPSocket

Interface: UDPSocket

NameTypeDescription
bind(port?): numberBind a system randomly assigned available port or bind a specified port number
close(): voidStop UDP Socket Instance. After close, UDPSocket instance can not send messages, and UDPSocket.send will trigger an error event.
connect(options): voidPreconnect to the specified IP and Port
send(payload): voidsend message to target IP and port
write(payload): voidsimilar to send
setTTL(ttl): voidset socket ttl
onClose(callback): voidregisiter callback for socket close
offClose(callback): voidunregisiter callback for socket close
onError(callback): voidregisiter callback for errors
offError(callback): voidunregisiter callback for errors
onListening(callback): voidregisiter callback when listener rigistered
offListening(callback): voidunregisiter callback when listener rigistered
onMessage(callback): voidregisiter callback when receive messages
offMessage(callback): voidunregisiter callback when receive messages

bind

bind(port?): number

Bind a system randomly assigned available port or bind a specified port number

Parameters

NameTypeDescription
port?numberSpecifies the port number to bind, or returns the randomly available port number if not specified

Returns

number

Successfully bound port number


close

close(): void

Stop UDP Socket Instance. After close, UDPSocket instance can not send messages, and UDPSocket.send will trigger an error event.

Returns

void


connect

connect(options): void

Preconnect to the specified IP and Port

Parameters

NameType
optionsConnectOptions

Returns

void


send

send(payload): void

send message to target IP and port

Parameters

NameType
payloadSendPaypoad

Returns

void


write

write(payload): void

similar to send

Parameters

NameType
payloadSendPaypoad

Returns

void


setTTL

setTTL(ttl): void

set socket ttl

Parameters

NameTypeDescription
ttlnumberttl value, range [0, 255]

Returns

void


onClose

onClose(callback): void

regisiter callback for socket close

Parameters

NameType
callback() => void

Returns

void


offClose

offClose(callback): void

unregisiter callback for socket close

Parameters

NameType
callback() => void

Returns

void


onError

onError(callback): void

regisiter callback for errors

Parameters

NameType
callbackErrorCallback

Returns

void


offError

offError(callback): void

unregisiter callback for errors

Parameters

NameType
callbackErrorCallback

Returns

void


onListening

onListening(callback): void

regisiter callback when listener rigistered

Parameters

NameType
callback() => void

Returns

void


offListening

offListening(callback): void

unregisiter callback when listener rigistered

Parameters

NameType
callback() => void

Returns

void


onMessage

onMessage(callback): void

regisiter callback when receive messages

Parameters

NameType
callbackMessageCallback

Returns

void


offMessage

offMessage(callback): void

unregisiter callback when receive messages

Parameters

NameType
callbackMessageCallback

Returns

void

ConnectOptions

Ƭ ConnectOptions: Object

Type declaration

NameTypeDescription
addressstringtarget ip address
portnumbertarget port

SendPaypoad

Ƭ SendPaypoad: Object

Type declaration

NameTypeDescription
addressstringtarget ip address
portnumbertarget port
messagestring | ArrayBuffermessage data
offset?numberoffset for arraybuffer, default 0
number?numbermessage length for arraybuffer, default message.byteLength
setBroadcast?booleanbroadcast the message, default false

ErrorCallback

Ƭ ErrorCallback: (res: { errMsg: string }) => void

Type declaration

▸ (res): void

Parameters

NameType
resObject
res.errMsgstring

Returns

void


MessageCallback

Ƭ MessageCallback: (payload: MessageCallbackPayload) => void

Type declaration

▸ (payload): void

Parameters

NameType
payloadMessageCallbackPayload

Returns

void


MessageCallbackPayload

Ƭ MessageCallbackPayload: Object

Type declaration

NameTypeDescription
messageArrayBuffermessage received
remoteInfo{ address: string ; family: string ; port: number ; size: number }remote address information
remoteInfo.addressstringremote socket address
remoteInfo.familystringthe protocol family IPv4 or IPv6
remoteInfo.portnumberport
remoteInfo.sizenumbermessage byte size
localInfo{ address: string ; family: string ; port: number }local address information
localInfo.addressstringlocal socket address
localInfo.familystringthe protocol family IPv4 or IPv6
localInfo.portnumberport