Packaged apps can act as a network client for TCP and UDP connections. This doc shows you how to use TCP and UDP to send and receive data over the network. For more information, see the Sockets API.
API Samples: Want to play with the code? Check out the telnet and udp samples.
For packaged apps that use TCP or UDP, add the "socket" permission to the manifest and specify the IP end point permission rules. For example:
"permissions": [
{"socket": [
"rule1",
"rule2",
...
]}
]
The syntax of socket permission rules follows these patterns:
<socket-permission-rule>
:= <op> | <op> ':' <host> | <op> ':' ':' <port> |
<op> ':' <host> ':' <port>
<op> := 'tcp-connect' | 'tcp-listen' | 'udp-bind' | 'udp-send-to'
<host> := '*' | '*.' <anychar except '/' and '*'>+
<port> := '*' | <port number between 1 and 65535>)
Examples of socket permission rules:
Packaged apps can make connections to any service that supports TCP.
Here's a sample showing how to connect ($ref:socket.connect) to a socket:
chrome.socket.create('tcp', {}, function(createInfo) {
chrome.socket.connect(createInfo.socketId, IP, PORT, onConnectedCallback);
});
Keep a handle to the socketId so that you can later read and write ($ref:socket.write) to this socket.
chrome.socket.write(socketId, arrayBuffer, onWriteCompleteCallback);
Reading ($ref:socket.read) and writing from a socket uses ArrayBuffer objects. To learn about ArrayBuffers, check out the overview, JavaScript typed arrays, and the tutorial, How to convert ArrayBuffer to and from String.
chrome.socket.read(socketId, null, function(readInfo) {
if (readInfo.resultCode > 0) {
// readInfo.data is an arrayBuffer.
}
});
Here's how to disconnect ($ref:socket.disconnect):
chrome.socket.disconnect(socketId);
Packaged apps can make connections to any service that supports UDP.
Here's a sample showing how to send data over the network using UDP:
// Create the Socket
chrome.socket.create('udp', '127.0.0.1', 1337, {},
function(socketInfo) {
// The socket is created, now we want to connect to the service
var socketId = socketInfo.socketId;
chrome.socket.connect(socketId, function(result) {
// We are now connected to the socket so send it some data
chrome.socket.write(socketId, arrayBuffer,
function(sendInfo) {
console.log("wrote " + sendInfo.bytesWritten);
}
);
});
}
);
This example is very similar to the 'Sending data' example with the addition of a special handler in the 'create' method. The parameter is an object with one value 'onEvent' that is a function reference to the method that will be called when data is available on the port.
// Handle the data response
var handleDataEvent = function(d) {
var data = chrome.socket.read(d.socketId);
console.log(data);
};
// Create the Socket
chrome.socket.create('udp', '127.0.0.1', 1337, { onEvent: handleDataEvent },
function(socketInfo) {
// The socket is created, now we want to connect to the service
var socketId = socketInfo.socketId;
chrome.socket.connect(socketId, function(result) {
// We are now connected to the socket so send it some data
chrome.socket.write(socketId, arrayBuffer,
function(sendInfo) {
console.log("wrote " + sendInfo.bytesWritten);
}
);
});
}
);