chrome.socket
- Description
Use the
chrome.socketAPI 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.tcpandsockets.tcpServerAPIs. - Permissions
socket - AvailabilityPending
Summary
- Types
- Methods
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
tcpsockets, 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
udpsockets, 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
tcporudp.
SocketType
Type
"tcp", or "udp"
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.
Methods
accept
chrome.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
callbackparameter looks like:(acceptInfo: AcceptInfo) => void- acceptInfo
bind
chrome.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
The
callbackparameter looks like:(result: number) => void- resultnumber
connect
chrome.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
The
callbackparameter looks like:(result: number) => void- resultnumber
create
chrome.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
tcporudp. - options
The socket options.
- callbackfunction
The
callbackparameter looks like:(createInfo: CreateInfo) => void- createInfo
destroy
chrome.socket.destroy(
socketId: number,
)Destroys the socket. Each socket created should be destroyed after use.
Parameters
- socketIdnumber
The socketId.
disconnect
chrome.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
chrome.socket.getInfo(
socketId: number,
callback: function,
)Retrieves the state of the given socket.
Parameters
- socketIdnumber
The socketId.
- callbackfunction
The
callbackparameter looks like:(result: SocketInfo) => void- result
getJoinedGroups
chrome.socket.getJoinedGroups(
socketId: number,
callback: function,
)Get the multicast group addresses the socket is currently joined to.
Parameters
- socketIdnumber
The socketId.
- callbackfunction
The
callbackparameter looks like:(groups: string[]) => void- groupsstring[]
getNetworkList
chrome.socket.getNetworkList(
callback: function,
)Retrieves information about local adapters on this system.
Parameters
- callbackfunction
The
callbackparameter looks like:(result: NetworkInterface[]) => void- result
joinGroup
chrome.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
The
callbackparameter looks like:(result: number) => void- resultnumber
leaveGroup
chrome.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
The
callbackparameter looks like:(result: number) => void- resultnumber
listen
chrome.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
Length of the socket's listen queue.
- callbackfunction
The
callbackparameter looks like:(result: number) => void- resultnumber
read
chrome.socket.read(
socketId: number,
bufferSize: number,
callback: function,
)Reads data from the given connected socket.
Parameters
- socketIdnumber
The socketId.
- bufferSizenumber
The read buffer size.
- callbackfunction
The
callbackparameter looks like:(readInfo: ReadInfo) => void- readInfo
recvFrom
chrome.socket.recvFrom(
socketId: number,
bufferSize: number,
callback: function,
)Receives data from the given UDP socket.
Parameters
- socketIdnumber
The socketId.
- bufferSizenumber
The receive buffer size.
- callbackfunction
The
callbackparameter looks like:(recvFromInfo: RecvFromInfo) => void- recvFromInfo
secure
chrome.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.
- options
Constraints and parameters for the TLS connection.
- callbackfunction
The
callbackparameter looks like:(result: number) => void- resultnumber
sendTo
chrome.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
The
callbackparameter looks like:(writeInfo: WriteInfo) => void- writeInfo
setKeepAlive
chrome.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
Set the delay seconds between the last data packet received and the first keepalive probe. Default is 0.
- callbackfunction
The
callbackparameter looks like:(result: boolean) => void- resultboolean
setMulticastLoopbackMode
chrome.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
The
callbackparameter looks like:(result: number) => void- resultnumber
setMulticastTimeToLive
chrome.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
The
callbackparameter looks like:(result: number) => void- resultnumber
setNoDelay
chrome.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
The
callbackparameter looks like:(result: boolean) => void- resultboolean
write
chrome.socket.write(
socketId: number,
data: ArrayBuffer,
callback: function,
)Writes data on the given connected socket.