1 <h1>Network Communications
</h1>
4 Chrome Apps can act as a network client
5 for TCP and UDP connections.
6 This doc shows you how to use TCP and UDP
7 to send and receive data over the network.
10 <a href=
"sockets_udp">Sockets UDP
</a>,
11 <a href=
"sockets_tcp">Sockets TCP
</a> and
12 <a href=
"sockets_tcp_server">Sockets TCP Server
</a> APIs.
16 <b>Note:
</b>The previous version of the networking APIs ($(ref:socket)) has been
22 Want to play with the code?
24 <a href=
"https://github.com/GoogleChrome/chrome-app-samples/tree/master/samples/telnet">telnet
</a>
25 and
<a href=
"https://github.com/GoogleChrome/chrome-app-samples/tree/master/samples/udp">udp
</a> samples.
28 <h2 id=
"manifest">Manifest requirements
</h2>
31 For Chrome Apps that use TCP or UDP,
32 add the
<a href=
"manifest/sockets">sockets
</a> entry to the manifest
33 and specify the IP end point permission rules.
37 <pre data-filename=
"manifest.json">
40 "send": [
"host-pattern1", ...],
41 "bind": [
"host-pattern2", ...],
45 "connect": [
"host-pattern1", ...],
49 "listen": [
"host-pattern1", ...],
56 The syntax of socket
"host-pattern" entries follows these rules:
60 <host-pattern
> :=
<host
> | ':'
<port
> |
<host
> ':'
<port
>
61 <host
> := '*' | '*.'
<anychar except '/' and '*'
>+
62 <port
> := '*' |
<port number between
1 and
65535>)
66 See
<a href=
"manifest/sockets">Sockets Manifest Key
</a> for detailed
67 description of the syntax.
71 Examples of socket manifest entries:
75 <li><code>{
"tcp": {
"connect" :
"*:23" } }
</code> – connecting on
76 port
23 of any hosts
</li>
77 <li><code>{
"tcp": {
"connect" : [
"*:23",
"*:80"] } }
</code> –
78 connecting on port
23 or
80 of any hosts
</li>
79 <li><code>{
"tcp": {
"connect" :
"www.example.com:23" } }
</code> –
80 connecting port
23 of
<em>www.example.com
</em></li>
81 <li><code>{
"tcp": {
"connect" :
"" } }
</code> – connecting any ports
83 <li><code>{
"udp": {
"send" :
":99" } }
</code> – sending UDP packet
84 to port
99 of any hosts
</li>
85 <li><code>{
"udp": {
"bind" :
":8899" } }
</code> – binding local port
86 8899 to receive UDP packets
</li>
87 <li><code>{
"tcpServer": {
"listen" :
":8080" } }
</code> – TCP
88 listening on local port
8080</li>
91 <h2 id=
"tcp">Using TCP
</h2>
94 Chrome Apps can make connections to any service that supports TCP.
97 <h3 id=
"connecting">Connecting to a socket
</h3>
100 Here's a sample showing how to connect
101 ($(ref:sockets.tcp.connect)) to a socket:
105 chrome.sockets.tcp.create({}, function(createInfo) {
106 chrome.sockets.tcp.connect(createInfo.socketId,
107 IP, PORT, onConnectedCallback);
112 Keep a handle to the
<code>socketId
</code> so that
113 you can later received and send data
114 ($(ref:sockets.tcp.send)) to this socket.
117 <h3 id=
"reading">Receiving from and sending to a socket
</h3>
120 Receiving from ($(ref:sockets.tcp.onReceive)) and sending to a socket uses
121 ArrayBuffer objects. To learn about ArrayBuffers, check out the overview,
122 <a href=
"https://developer.mozilla.org/en-US/docs/JavaScript_typed_arrays">JavaScript typed arrays
</a>,
124 <a href=
"http://updates.html5rocks.com/2012/06/How-to-convert-ArrayBuffer-to-and-from-String">How to convert ArrayBuffer to and from String
</a>.
128 chrome.sockets.tcp.send(socketId, arrayBuffer, onSentCallback);
132 chrome.sockets.tcp.onReceive.addListener(function(info) {
133 if (info.socketId != socketId)
135 // info.data is an arrayBuffer.
139 <h3 id=
"disconnecting">Disconnecting from a socket
</h3>
141 <p>Here's how to disconnect ($(ref:sockets.tcp.disconnect)):
</p>
143 <pre>chrome.sockets.tcp.disconnect(socketId);
</pre>
145 <h2 id=
"udp">Using UDP
</h2>
148 Chrome Apps can make connections to any service that supports UDP.
151 <h3 id=
"sending">Sending data
</h3>
154 Here's a sample showing how to send data ($(ref:sockets.udp.send))
155 over the network using UDP:
160 chrome.sockets.udp.create({}, function(socketInfo) {
161 // The socket is created, now we can send some data
162 var socketId = socketInfo.socketId;
163 chrome.sockets.udp.send(socketId, arrayBuffer,
164 '
127.0.0.1',
1337, function(sendInfo) {
165 console.log(
"sent " + sendInfo.bytesSent);
170 <h3 id=
"receiving">Receiving data
</h3>
173 This example is very similar to the 'Sending data' example, except we
174 setup an event handler for receiving data.
180 // Handle the
"onReceive" event.
181 var onReceive = function(info) {
182 if (info.socketId !== socketId)
184 console.log(info.data);
188 chrome.sockets.udp.create({}, function(socketInfo) {
189 socketId = socketInfo.socketId;
190 // Setup event handler and bind socket.
191 chrome.sockets.udp.onReceive.addListener(onReceive);
192 chrome.sockets.udp.bind(socketId,
193 "0.0.0.0",
0, function(result) {
195 console.log(
"Error binding socket.");
198 chrome.sockets.udp.send(socketId, arrayBuffer,
199 '
127.0.0.1',
1337, function(sendInfo) {
200 console.log(
"sent " + sendInfo.bytesSent);
206 <h2 id=
"tcpServer">Using TCP Server
</h2>
209 Chrome Apps can act as TCP servers using the $(ref:sockets.tcpServer) API.
212 <h3 id=
"creating-server">Creating a TCP server socket
</h3>
215 Create a TCP server socket with $(ref:sockets.tcpServer.create).
219 chrome.sockets.tcpServer.create({}, function(createInfo) {
220 listenAndAccept(createInfo.socketId);
224 <h3 id=
"accepting">Accepting client connections
</h3>
227 Here's a sample showing how to accept connections
228 ($(ref:sockets.tcpServer.listen)) on a TCP server socket:
232 function listenAndAccept(socketId) {
233 chrome.sockets.tcpServer.listen(socketId,
234 IP, PORT, function(resultCode) {
235 onListenCallback(socketId, resultCode)
241 Keep a handle to the
<code>socketId
</code> so that
242 you can later accept new connections
243 ($(ref:sockets.tcpServer.onAccept)) .
248 function onListenCallback(socketId, resultCode) {
249 if (resultCode <
0) {
250 console.log(
"Error listening:" +
251 chrome.runtime.lastError.message);
254 serverSocketId = socketId;
255 chrome.sockets.tcpServer.onAccept.addListener(onAccept)
260 When a new connection is established,
<code>onAccept
</code> is invoked with
261 the
<code>clientSocketId
</code> of the new TCP connection. The client socket ID
262 must be used with the $(ref:sockets.tcp) API.
263 The socket of the new connection is paused by default. Un-pause it with
264 $(ref:sockets.tcp.setPaused) to start receiving data.
268 function onAccept(info) {
269 if (info.socketId != serverSocketId)
272 // A new TCP connection has been established.
273 chrome.sockets.tcp.send(info.clientSocketId, data,
274 function(resultCode) {
275 console.log(
"Data sent to new TCP client connection.")
277 // Start receiving data.
278 chrome.sockets.tcp.onReceive.addListener(function(recvInfo) {
279 if (recvInfo.socketId != info.clientSocketId)
281 // recvInfo.data is an arrayBuffer.
283 chrome.sockets.tcp.setPaused(false);
287 <h3 id=
"stop-accepting">Stop accepting client connections
</h3>
290 Call $(ref:sockets.tcp.disconnect) on the server socket ID to stop accepting
295 chrome.sockets.tcpServer.onAccept.removeListener(onAccept);
296 chrome.sockets.tcpServer.disconnect(serverSocketId);
</pre>
299 <p class=
"backtotop"><a href=
"#top">Back to top
</a></p>