chrome.socket
- Description
Use the
chrome.socket
API to send and receive data over the network using TCP and UDP connections. Note: Starting with Chrome 33, this API is deprecated in favor of thesockets.udp
,sockets.tcp
andsockets.tcpServer
APIs. - Permissions
socket
Summary
- Types
- Methods
socket.accept(socketId: number, callback: function)
socket.bind(socketId: number, address: string, port: number, callback: function)
socket.connect(socketId: number, hostname: string, port: number, callback: function)
socket.create(type: SocketType, options?: CreateOptions, callback: function)
socket.destroy(socketId: number)
socket.disconnect(socketId: number)
socket.getInfo(socketId: number, callback: function)
socket.getJoinedGroups(socketId: number, callback: function)
socket.getNetworkList(callback: function)
socket.joinGroup(socketId: number, address: string, callback: function)
socket.leaveGroup(socketId: number, address: string, callback: function)
socket.listen(socketId: number, address: string, port: number, backlog?: number, callback: function)
socket.read(socketId: number, bufferSize?: number, callback: function)
socket.recvFrom(socketId: number, bufferSize?: number, callback: function)
socket.secure(socketId: number, options?: SecureOptions, callback: function)
socket.sendTo(socketId: number, data: ArrayBuffer, address: string, port: number, callback: function)
socket.setKeepAlive(socketId: number, enable: boolean, delay?: number, callback: function)
socket.setMulticastLoopbackMode(socketId: number, enabled: boolean, callback: function)
socket.setMulticastTimeToLive(socketId: number, ttl: number, callback: function)
socket.setNoDelay(socketId: number, noDelay: boolean, callback: function)
socket.write(socketId: number, data: ArrayBuffer, callback: function)
Types
AcceptInfo
Properties
- resultCodenumber
- socketIdnumber optional
The id of the accepted socket.
CreateInfo
Properties
- socketIdnumber
The id of the newly created socket.
CreateOptions
NetworkInterface
Properties
- addressstring
The available IPv4/6 address.
- namestring
The underlying name of the adapter. On *nix, this will typically be "eth0", "lo", etc.
- prefixLengthnumber
The prefix length
ReadInfo
Properties
- dataArrayBuffer
- resultCodenumber
The resultCode returned from the underlying read() call.
RecvFromInfo
Properties
- addressstring
The address of the remote machine.
- dataArrayBuffer
- portnumber
- resultCodenumber
The resultCode returned from the underlying recvfrom() call.
SecureOptions
Properties
- tlsVersionTLSVersionConstraints optional
SocketInfo
Properties
- connectedboolean
Whether or not the underlying socket is connected.
For
tcp
sockets, this will remain true even if the remote peer has disconnected. Reading or writing to the socket may then result in an error, hinting that this socket should be disconnected viadisconnect()
.For
udp
sockets, this just represents whether a default remote address has been specified for reading and writing packets. - localAddressstring optional
If the underlying socket is bound or connected, contains its local IPv4/6 address.
- localPortnumber optional
If the underlying socket is bound or connected, contains its local port.
- peerAddressstring optional
If the underlying socket is connected, contains the IPv4/6 address of the peer.
- peerPortnumber optional
If the underlying socket is connected, contains the port of the connected peer.
- socketType
The type of the passed socket. This will be
tcp
orudp
.
TLSVersionConstraints
Properties
- maxstring optional
- minstring optional
The minimum and maximum acceptable versions of TLS. These will be
tls1
,tls1.1
,tls1.2
, ortls1.3
.
WriteInfo
Properties
- bytesWrittennumber
The number of bytes sent, or a negative error code.
SocketType
Enum
"tcp"
, or "udp"
Methods
accept
socket.accept(socketId: number, callback: function)
This method applies to TCP sockets only. Registers a callback function to be called when a connection is accepted on this listening server socket. Listen must be called first. If there is already an active accept callback, this callback will be invoked immediately with an error as the resultCode.
Parameters
- socketIdnumber
The socketId.
- callbackfunction
The callback is invoked when a new socket is accepted.
The callback parameter should be a function that looks like this:
(acceptInfo: AcceptInfo) => {...}
- acceptInfo
bind
socket.bind(socketId: number, address: string, port: number, callback: function)
Binds the local address for socket. Currently, it does not support TCP socket.
Parameters
- socketIdnumber
The socketId.
- addressstring
The address of the local machine.
- portnumber
The port of the local machine.
- callbackfunction
Called when the bind attempt is complete.
The callback parameter should be a function that looks like this:
(result: number) => {...}
- resultnumber
connect
socket.connect(socketId: number, hostname: string, port: number, callback: function)
Connects the socket to the remote machine (for a tcp
socket). For a udp
socket, this sets the default address which packets are sent to and read from for read()
and write()
calls.
Parameters
- socketIdnumber
The socketId.
- hostnamestring
The hostname or IP address of the remote machine.
- portnumber
The port of the remote machine.
- callbackfunction
Called when the connection attempt is complete.
The callback parameter should be a function that looks like this:
(result: number) => {...}
- resultnumber
create
socket.create(type: SocketType, options?: CreateOptions, callback: function)
Creates a socket of the specified type that will connect to the specified remote machine.
Parameters
- type
The type of socket to create. Must be
tcp
orudp
. - optionsCreateOptions optional
The socket options.
- callbackfunction
Called when the socket has been created.
The callback parameter should be a function that looks like this:
(createInfo: CreateInfo) => {...}
- createInfo
destroy
socket.destroy(socketId: number)
Destroys the socket. Each socket created should be destroyed after use.
Parameters
- socketIdnumber
The socketId.
disconnect
socket.disconnect(socketId: number)
Disconnects the socket. For UDP sockets, disconnect
is a non-operation but is safe to call.
Parameters
- socketIdnumber
The socketId.
getInfo
socket.getInfo(socketId: number, callback: function)
Retrieves the state of the given socket.
Parameters
- socketIdnumber
The socketId.
- callbackfunction
Called when the state is available.
The callback parameter should be a function that looks like this:
(result: SocketInfo) => {...}
- result
getJoinedGroups
socket.getJoinedGroups(socketId: number, callback: function)
Get the multicast group addresses the socket is currently joined to.
Parameters
- socketIdnumber
The socketId.
- callbackfunction
Called with an array of strings of the result.
The callback parameter should be a function that looks like this:
(groups: string[]) => {...}
- groupsstring[]
getNetworkList
socket.getNetworkList(callback: function)
Retrieves information about local adapters on this system.
Parameters
- callbackfunction
Called when local adapter information is available.
The callback parameter should be a function that looks like this:
(result: NetworkInterface[]) => {...}
- result
joinGroup
socket.joinGroup(socketId: number, address: string, callback: function)
Join the multicast group and start to receive packets from that group. The socket must be of UDP type and must be bound to a local port before calling this method.
Parameters
- socketIdnumber
The socketId.
- addressstring
The group address to join. Domain names are not supported.
- callbackfunction
Called when the join group operation is done with an integer parameter indicating the platform-independent error code.
The callback parameter should be a function that looks like this:
(result: number) => {...}
- resultnumber
leaveGroup
socket.leaveGroup(socketId: number, address: string, callback: function)
Leave the multicast group previously joined using joinGroup
. It's not necessary to leave the multicast group before destroying the socket or exiting. This is automatically called by the OS.
Leaving the group will prevent the router from sending multicast datagrams to the local host, presuming no other process on the host is still joined to the group.
Parameters
- socketIdnumber
The socketId.
- addressstring
The group address to leave. Domain names are not supported.
- callbackfunction
Called when the leave group operation is done with an integer parameter indicating the platform-independent error code.
The callback parameter should be a function that looks like this:
(result: number) => {...}
- resultnumber
listen
socket.listen(socketId: number, address: string, port: number, backlog?: number, callback: function)
This method applies to TCP sockets only. Listens for connections on the specified port and address. This effectively makes this a server socket, and client socket functions (connect, read, write) can no longer be used on this socket.
Parameters
- socketIdnumber
The socketId.
- addressstring
The address of the local machine.
- portnumber
The port of the local machine.
- backlognumber optional
Length of the socket's listen queue.
- callbackfunction
Called when listen operation completes.
The callback parameter should be a function that looks like this:
(result: number) => {...}
- resultnumber
read
socket.read(socketId: number, bufferSize?: number, callback: function)
Reads data from the given connected socket.
recvFrom
socket.recvFrom(socketId: number, bufferSize?: number, callback: function)
Receives data from the given UDP socket.
Parameters
- socketIdnumber
The socketId.
- bufferSizenumber optional
The receive buffer size.
- callbackfunction
Returns result of the recvFrom operation.
The callback parameter should be a function that looks like this:
(recvFromInfo: RecvFromInfo) => {...}
- recvFromInfo
secure
socket.secure(socketId: number, options?: SecureOptions, callback: function)
Start a TLS client connection over a connected TCP client socket.
Parameters
- socketIdnumber
The connected socket to use.
- optionsSecureOptions optional
Constraints and parameters for the TLS connection.
- callbackfunction
Called when the TLS connection attempt is complete.
The callback parameter should be a function that looks like this:
(result: number) => {...}
- resultnumber
sendTo
socket.sendTo(socketId: number, data: ArrayBuffer, address: string, port: number, callback: function)
Sends data on the given UDP socket to the given address and port.
Parameters
- socketIdnumber
The socketId.
- dataArrayBuffer
The data to write.
- addressstring
The address of the remote machine.
- portnumber
The port of the remote machine.
- callbackfunction
setKeepAlive
socket.setKeepAlive(socketId: number, enable: boolean, delay?: number, callback: function)
Enables or disables the keep-alive functionality for a TCP connection.
Parameters
- socketIdnumber
The socketId.
- enableboolean
If true, enable keep-alive functionality.
- delaynumber optional
Set the delay seconds between the last data packet received and the first keepalive probe. Default is 0.
- callbackfunction
Called when the setKeepAlive attempt is complete.
The callback parameter should be a function that looks like this:
(result: boolean) => {...}
- resultboolean
setMulticastLoopbackMode
socket.setMulticastLoopbackMode(socketId: number, enabled: boolean, callback: function)
Set whether multicast packets sent from the host to the multicast group will be looped back to the host.
Note: the behavior of setMulticastLoopbackMode
is slightly different between Windows and Unix-like systems. The inconsistency happens only when there is more than one application on the same host joined to the same multicast group while having different settings on multicast loopback mode. On Windows, the applications with loopback off will not RECEIVE the loopback packets; while on Unix-like systems, the applications with loopback off will not SEND the loopback packets to other applications on the same host. See MSDN: http://goo.gl/6vqbj
Calling this method does not require multicast permissions.
Parameters
- socketIdnumber
The socketId.
- enabledboolean
Indicate whether to enable loopback mode.
- callbackfunction
Called when the configuration operation is done.
The callback parameter should be a function that looks like this:
(result: number) => {...}
- resultnumber
setMulticastTimeToLive
socket.setMulticastTimeToLive(socketId: number, ttl: number, callback: function)
Set the time-to-live of multicast packets sent to the multicast group.
Calling this method does not require multicast permissions.
Parameters
- socketIdnumber
The socketId.
- ttlnumber
The time-to-live value.
- callbackfunction
Called when the configuration operation is done.
The callback parameter should be a function that looks like this:
(result: number) => {...}
- resultnumber
setNoDelay
socket.setNoDelay(socketId: number, noDelay: boolean, callback: function)
Sets or clears TCP_NODELAY
for a TCP connection. Nagle's algorithm will be disabled when TCP_NODELAY
is set.
Parameters
- socketIdnumber
The socketId.
- noDelayboolean
If true, disables Nagle's algorithm.
- callbackfunction
Called when the setNoDelay attempt is complete.
The callback parameter should be a function that looks like this:
(result: boolean) => {...}
- resultboolean
write
socket.write(socketId: number, data: ArrayBuffer, callback: function)
Writes data on the given connected socket.