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=
"socket.html">Sockets API
</a>.
15 Want to play with the code?
17 <a href=
"https://github.com/GoogleChrome/chrome-app-samples/tree/master/telnet">telnet
</a>
18 and
<a href=
"https://github.com/GoogleChrome/chrome-app-samples/tree/master/udp">udp
</a> samples.
21 <h2 id=
"manifest">Manifest requirements
</h2>
24 For Chrome Apps that use TCP or UDP,
25 add the
"socket" permission to the manifest
26 and specify the IP end point permission rules.
30 <pre data-filename=
"manifest.json">
41 The syntax of socket permission rules follows these patterns:
45 <socket-permission-rule
>
46 :=
<op
> |
<op
> ':'
<host
> |
<op
> ':' ':'
<port
> |
47 <op
> ':'
<host
> ':'
<port
>
48 <op
> := 'tcp-connect' | 'tcp-listen' | 'udp-bind' | 'udp-send-to'
49 <host
> := '*' | '*.'
<anychar except '/' and '*'
>+
50 <port
> := '*' |
<port number between
1 and
65535>)
54 Examples of socket permission rules:
58 <li>"tcp-connect:*:23" – connecting on port
23 of any hosts
</li>
59 <li>"tcp-connect:www.example.com:23" – connecting port
23 of
<em>www.example.com
</em></li>
60 <li>"tcp-connect" – connecting any ports of any hosts
</li>
61 <li>"udp-send-to::99" – sending UDP packet to port
99 of any hosts
</li>
62 <li>"udp-bind::8899" – binding local port
8899 to receive UDP package
</li>
63 <li>"tcp-listen::8080" – TCP listening on local port
8080</li>
66 <h2 id=
"tcp">Using TCP
</h2>
69 Chrome Apps can make connections to any service that supports TCP.
72 <h3 id=
"connecting">Connecting to a socket
</h3>
75 Here's a sample showing how to connect
76 ($ref:socket.connect) to a socket:
80 chrome.socket.create('tcp', {}, function(createInfo) {
81 chrome.socket.connect(createInfo.socketId, IP, PORT, onConnectedCallback);
86 Keep a handle to the socketId so that
87 you can later read and write
88 ($ref:socket.write) to this socket.
92 chrome.socket.write(socketId, arrayBuffer, onWriteCompleteCallback);
95 <h3 id=
"reading">Reading to & writing from a socket
</h3>
98 Reading ($ref:socket.read) and writing from a socket uses ArrayBuffer objects.
99 To learn about ArrayBuffers,
100 check out the overview,
101 <a href=
"https://developer.mozilla.org/en-US/docs/JavaScript_typed_arrays">JavaScript typed arrays
</a>,
103 <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>.
107 chrome.socket.read(socketId, null, function(readInfo) {
108 if (readInfo.resultCode
> 0) {
109 // readInfo.data is an arrayBuffer.
114 <h3 id=
"disconnecting">Disconnecting from a socket
</h3>
116 <p>Here's how to disconnect ($ref:socket.disconnect):
</p>
118 <pre>chrome.socket.disconnect(socketId);
</pre>
120 <h2 id=
"udp">Using UDP
</h2>
123 Chrome Apps can make connections to any service that supports UDP.
126 <h3 id=
"sending">Sending data
</h3>
129 Here's a sample showing how to send data
130 over the network using UDP:
135 chrome.socket.create('udp', '
127.0.0.1',
1337, {},
136 function(socketInfo) {
137 // The socket is created, now we want to connect to the service
138 var socketId = socketInfo.socketId;
139 chrome.socket.connect(socketId, function(result) {
140 // We are now connected to the socket so send it some data
141 chrome.socket.write(socketId, arrayBuffer,
143 console.log(
"wrote " + sendInfo.bytesWritten);
151 <h3 id=
"receiving">Receiving data
</h3>
154 This example is very similar to the 'Sending data' example
155 with the addition of a special handler in the 'create' method.
156 The parameter is an object with one value 'onEvent'
157 that is a function reference to the method
158 that will be called when data is available on the port.
162 // Handle the data response
163 var handleDataEvent = function(d) {
164 var data = chrome.socket.read(d.socketId);
169 chrome.socket.create('udp', '
127.0.0.1',
1337, { onEvent: handleDataEvent },
170 function(socketInfo) {
171 // The socket is created, now we want to connect to the service
172 var socketId = socketInfo.socketId;
173 chrome.socket.connect(socketId, function(result) {
174 // We are now connected to the socket so send it some data
175 chrome.socket.write(socketId, arrayBuffer,
177 console.log(
"wrote " + sendInfo.bytesWritten);
185 <p class=
"backtotop"><a href=
"#top">Back to top
</a></p>