Socket

external class Socket : EventEmitter(source)

Encapsulates the datagram functionality.

New instances of dgram.Socket are created using {@link createSocket}. The new keyword is not to be used to create dgram.Socket instances.

Since

v0.1.99

Constructors

Link copied to clipboard
constructor()

Functions

Link copied to clipboard
fun addListener(event: String, listener: Function<Unit>)

events.EventEmitter

fun addListener(event: SocketEvent.CLOSE, listener: () -> Unit)
fun addListener(event: SocketEvent.CONNECT, listener: () -> Unit)
fun addListener(event: SocketEvent.ERROR, listener: (JsError) -> Unit)
fun addListener(event: SocketEvent.LISTENING, listener: () -> Unit)
fun addListener(event: SocketEvent.MESSAGE, listener: (msg: Buffer, rinfo: RemoteInfo) -> Unit)
Link copied to clipboard
fun addMembership(multicastAddress: String, multicastInterface: String = definedExternally)

Tells the kernel to join a multicast group at the given multicastAddress and multicastInterface using the IP_ADD_MEMBERSHIP socket option. If the multicastInterface argument is not specified, the operating system will choose one interface and will add membership to it. To add membership to every available interface, call addMembership multiple times, once per interface.

Link copied to clipboard

Returns an object containing the address information for a socket. For UDP sockets, this object will contain address, family, and port properties.

Link copied to clipboard
fun addSourceSpecificMembership(sourceAddress: String, groupAddress: String, multicastInterface: String = definedExternally)

Tells the kernel to join a source-specific multicast channel at the given sourceAddress and groupAddress, using the multicastInterface with the IP_ADD_SOURCE_MEMBERSHIP socket option. If the multicastInterface argument is not specified, the operating system will choose one interface and will add membership to it. To add membership to every available interface, call socket.addSourceSpecificMembership() multiple times, once per interface.

Link copied to clipboard
fun bind(callback: () -> Unit = definedExternally)
fun bind(port: Number = definedExternally, callback: () -> Unit = definedExternally)
fun bind(options: BindOptions, callback: () -> Unit = definedExternally)

fun bind(port: Number = definedExternally, address: String = definedExternally, callback: () -> Unit = definedExternally)

For UDP sockets, causes the dgram.Socket to listen for datagram messages on a named port and optional address. If port is not specified or is 0, the operating system will attempt to bind to a random port. If address is not specified, the operating system will attempt to listen on all addresses. Once binding is complete, a 'listening' event is emitted and the optional callback function is called.

Link copied to clipboard
fun close(callback: () -> Unit = definedExternally)

Close the underlying socket and stop listening for data on it. If a callback is provided, it is added as a listener for the 'close' event.

Link copied to clipboard
fun connect(port: Number, callback: () -> Unit)

fun connect(port: Number, address: String = definedExternally, callback: () -> Unit = definedExternally)

Associates the dgram.Socket to a remote address and port. Every message sent by this handle is automatically sent to that destination. Also, the socket will only receive messages from that remote peer. Trying to call connect() on an already connected socket will result in an ERR_SOCKET_DGRAM_IS_CONNECTED exception. If address is not provided, '127.0.0.1' (for udp4 sockets) or '::1' (for udp6 sockets) will be used by default. Once the connection is complete, a 'connect' event is emitted and the optional callback function is called. In case of failure, the callback is called or, failing this, an 'error' event is emitted.

Link copied to clipboard

A synchronous function that disassociates a connected dgram.Socket from its remote address. Trying to call disconnect() on an unbound or already disconnected socket will result in an ERR_SOCKET_DGRAM_NOT_CONNECTED exception.

Link copied to clipboard
fun dropMembership(multicastAddress: String, multicastInterface: String = definedExternally)

Instructs the kernel to leave a multicast group at multicastAddress using the IP_DROP_MEMBERSHIP socket option. This method is automatically called by the kernel when the socket is closed or the process terminates, so most apps will never have reason to call this.

Link copied to clipboard
fun dropSourceSpecificMembership(sourceAddress: String, groupAddress: String, multicastInterface: String = definedExternally)

Instructs the kernel to leave a source-specific multicast channel at the given sourceAddress and groupAddress using the IP_DROP_SOURCE_MEMBERSHIP socket option. This method is automatically called by the kernel when the socket is closed or the process terminates, so most apps will never have reason to call this.

Link copied to clipboard
fun emit(event: Symbol, vararg args: Any?): Boolean
fun emit(event: String, vararg args: Any?): Boolean
fun emit(event: SocketEvent.MESSAGE, msg: Buffer, rinfo: RemoteInfo): Boolean
Link copied to clipboard

Returns an array listing the events for which the emitter has registered listeners. The values in the array are strings or Symbols.

Link copied to clipboard

Returns the current max listener value for the EventEmitter which is either set by emitter.setMaxListeners(n) or defaults to {@link defaultMaxListeners}.

Link copied to clipboard

This method throws ERR_SOCKET_BUFFER_SIZE if called on an unbound socket.

Link copied to clipboard

This method throws ERR_SOCKET_BUFFER_SIZE if called on an unbound socket.

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
fun on(event: String, listener: Function<Unit>)
fun on(event: SocketEvent.CLOSE, listener: () -> Unit)
fun on(event: SocketEvent.CONNECT, listener: () -> Unit)
fun on(event: SocketEvent.ERROR, listener: (JsError) -> Unit)
fun on(event: SocketEvent.LISTENING, listener: () -> Unit)
fun on(event: SocketEvent.MESSAGE, listener: (msg: Buffer, rinfo: RemoteInfo) -> Unit)
Link copied to clipboard
fun once(event: String, listener: Function<Unit>)
fun once(event: SocketEvent.CLOSE, listener: () -> Unit)
fun once(event: SocketEvent.CONNECT, listener: () -> Unit)
fun once(event: SocketEvent.ERROR, listener: (JsError) -> Unit)
fun once(event: SocketEvent.LISTENING, listener: () -> Unit)
fun once(event: SocketEvent.MESSAGE, listener: (msg: Buffer, rinfo: RemoteInfo) -> Unit)
Link copied to clipboard
fun prependListener(event: String, listener: Function<Unit>)
fun prependListener(event: SocketEvent.CLOSE, listener: () -> Unit)
fun prependListener(event: SocketEvent.CONNECT, listener: () -> Unit)
fun prependListener(event: SocketEvent.ERROR, listener: (JsError) -> Unit)
fun prependListener(event: SocketEvent.LISTENING, listener: () -> Unit)
fun prependListener(event: SocketEvent.MESSAGE, listener: (msg: Buffer, rinfo: RemoteInfo) -> Unit)
Link copied to clipboard
fun prependOnceListener(event: String, listener: Function<Unit>)
fun prependOnceListener(event: SocketEvent.CLOSE, listener: () -> Unit)
fun prependOnceListener(event: SocketEvent.CONNECT, listener: () -> Unit)
fun prependOnceListener(event: SocketEvent.LISTENING, listener: () -> Unit)
fun prependOnceListener(event: SocketEvent.MESSAGE, listener: (msg: Buffer, rinfo: RemoteInfo) -> Unit)
Link copied to clipboard
fun ref()

By default, binding a socket will cause it to block the Node.js process from exiting as long as the socket is open. The socket.unref() method can be used to exclude the socket from the reference counting that keeps the Node.js process active. The socket.ref() method adds the socket back to the reference counting and restores the default behavior.

Link copied to clipboard

Returns an object containing the address, family, and port of the remote endpoint. This method throws an ERR_SOCKET_DGRAM_NOT_CONNECTED exception if the socket is not connected.

Link copied to clipboard
fun send(msg: Uint8Array, callback: (JsError?, bytes: Double) -> Unit = definedExternally)
fun send(msg: ReadonlyArray<Any?>, callback: (JsError?, bytes: Double) -> Unit = definedExternally)
fun send(msg: String, callback: (JsError?, bytes: Double) -> Unit = definedExternally)
fun send(msg: Uint8Array, port: Number = definedExternally, callback: (JsError?, bytes: Double) -> Unit = definedExternally)
fun send(msg: ReadonlyArray<Any?>, port: Number = definedExternally, callback: (JsError?, bytes: Double) -> Unit = definedExternally)
fun send(msg: String, port: Number = definedExternally, callback: (JsError?, bytes: Double) -> Unit = definedExternally)
fun send(msg: Uint8Array, offset: Number, length: Number, callback: (JsError?, bytes: Double) -> Unit = definedExternally)
fun send(msg: String, offset: Number, length: Number, callback: (JsError?, bytes: Double) -> Unit = definedExternally)
fun send(msg: Uint8Array, offset: Number, length: Number, port: Number = definedExternally, callback: (JsError?, bytes: Double) -> Unit = definedExternally)
fun send(msg: String, offset: Number, length: Number, port: Number = definedExternally, callback: (JsError?, bytes: Double) -> Unit = definedExternally)
fun send(msg: Uint8Array, offset: Number, length: Number, port: Number = definedExternally, address: String = definedExternally, callback: (JsError?, bytes: Double) -> Unit = definedExternally)
fun send(msg: String, offset: Number, length: Number, port: Number = definedExternally, address: String = definedExternally, callback: (JsError?, bytes: Double) -> Unit = definedExternally)

fun send(msg: Uint8Array, port: Number = definedExternally, address: String = definedExternally, callback: (JsError?, bytes: Double) -> Unit = definedExternally)
fun send(msg: ReadonlyArray<Any?>, port: Number = definedExternally, address: String = definedExternally, callback: (JsError?, bytes: Double) -> Unit = definedExternally)
fun send(msg: String, port: Number = definedExternally, address: String = definedExternally, callback: (JsError?, bytes: Double) -> Unit = definedExternally)

Broadcasts a datagram on the socket. For connectionless sockets, the destination port and address must be specified. Connected sockets, on the other hand, will use their associated remote endpoint, so the port and address arguments must not be set.

Link copied to clipboard

Sets or clears the SO_BROADCAST socket option. When set to true, UDP packets may be sent to a local interface's broadcast address.

Link copied to clipboard

By default EventEmitters will print a warning if more than 10 listeners are added for a particular event. This is a useful default that helps finding memory leaks. The emitter.setMaxListeners() method allows the limit to be modified for this specific EventEmitter instance. The value can be set to Infinity (or 0) to indicate an unlimited number of listeners.

Link copied to clipboard
fun setMulticastInterface(multicastInterface: String)

All references to scope in this section are referring to IPv6 Zone Indices, which are defined by RFC * 4007. In string form, an IP with a scope index is written as 'IP%scope' where scope is an interface name or interface number.

Link copied to clipboard

Sets or clears the IP_MULTICAST_LOOP socket option. When set to true, multicast packets will also be received on the local interface.

Link copied to clipboard

Sets the IP_MULTICAST_TTL socket option. While TTL generally stands for "Time to Live", in this context it specifies the number of IP hops that a packet is allowed to travel through, specifically for multicast traffic. Each router or gateway that forwards a packet decrements the TTL. If the TTL is decremented to 0 by a router, it will not be forwarded.

Link copied to clipboard

Sets the SO_RCVBUF socket option. Sets the maximum socket receive buffer in bytes.

Link copied to clipboard

Sets the SO_SNDBUF socket option. Sets the maximum socket send buffer in bytes.

Link copied to clipboard
fun setTTL(ttl: Number): Double

Sets the IP_TTL socket option. While TTL generally stands for "Time to Live", in this context it specifies the number of IP hops that a packet is allowed to travel through. Each router or gateway that forwards a packet decrements the TTL. If the TTL is decremented to 0 by a router, it will not be forwarded. Changing TTL values is typically done for network probes or when multicasting.

Link copied to clipboard
fun unref()

By default, binding a socket will cause it to block the Node.js process from exiting as long as the socket is open. The socket.unref() method can be used to exclude the socket from the reference counting that keeps the Node.js process active, allowing the process to exit even if the socket is still listening.