OLD | NEW |
---|---|
1 <h1>Network Communications</h1> | 1 <h1>Network Communications</h1> |
2 | 2 |
3 | |
4 <p> | 3 <p> |
5 Packaged apps can act as a network client | 4 Packaged apps can act as a network client |
6 for TCP and UDP connections. | 5 for TCP and UDP connections. |
7 This doc shows you how to use TCP and UDP | 6 This doc shows you how to use TCP and UDP |
8 to send and receive data over the network. | 7 to send and receive data over the network. |
9 For more information, | 8 For more information, |
10 see the | 9 see the |
11 <a href="experimental.socket.html">Sockets API</a>. | 10 <a href="socket.html">Sockets API</a>. |
12 </p> | 11 </p> |
13 | 12 |
14 <p class="note"> | 13 <p class="note"> |
15 <b>API Samples: </b> | 14 <b>API Samples: </b> |
16 Want to play with the code? | 15 Want to play with the code? |
17 Check out the | 16 Check out the |
18 <a href="https://github.com/GoogleChrome/chrome-app-samples/tree/master/telnet"> telnet</a> | 17 <a href="https://github.com/GoogleChrome/chrome-app-samples/tree/master/telnet"> telnet</a> |
19 and <a href="https://github.com/GoogleChrome/chrome-app-samples/tree/master/udp" >udp</a> samples. | 18 and <a href="https://github.com/GoogleChrome/chrome-app-samples/tree/master/udp" >udp</a> samples. |
20 </p> | 19 </p> |
21 | 20 |
22 <h2 id="manifest">Manifest requirements</h2> | 21 <h2 id="manifest">Manifest requirements</h2> |
23 | 22 |
24 <p> | 23 <p> |
25 For packaged apps that use TCP or UDP, | 24 For packaged apps that use TCP or UDP, |
26 add the "experimental" and "socket" permissions | 25 add the "socket" permission to the manifest |
27 to the manifest: | 26 and specify the IP end point permission rules. |
27 For example: | |
28 </p> | 28 </p> |
29 | 29 |
30 <pre> | 30 <pre> |
31 "permissions": [ | 31 "permissions": [ |
32 "experimental", | 32 {"socket": [ |
33 "socket" | 33 "rule1", |
34 ] | 34 "rule2", |
35 ... | |
36 ]} | |
37 ] | |
35 </pre> | 38 </pre> |
36 | 39 |
40 <p> | |
41 The syntax of socket permission rules follows these patterns: | |
42 </p> | |
43 | |
44 <pre> | |
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>) | |
51 </pre> | |
52 | |
53 <p> | |
54 Examples of socket permission rules: | |
55 </p> | |
56 | |
57 <ul> | |
58 <li>"tcp-connect:*:23" – For connecting port 23 of any hosts</li> | |
miket_OOO
2012/08/31 22:33:59
connecting to (or maybe on)
mkearney1
2012/08/31 23:32:12
Done.
| |
59 <li>"tcp-connect:www.example.com:23" – | |
60 For connecting port 23 of <em>www.example.com</em></li> | |
miket_OOO
2012/08/31 22:33:59
Overall I'd eliminate the "for" from each of these
mkearney1
2012/08/31 23:32:12
Done.
| |
61 <li>"tcp-connect" – For connecting any ports of any hosts</li> | |
62 <li>"udp-send-to::99" – For sending UDP package to port 99 of any hosts< /li> | |
miket_OOO
2012/08/31 22:33:59
packet, not package, and I'd say "sending UDP pack
mkearney1
2012/08/31 23:32:12
Done.
| |
63 <li>"udp-bind::8899" – For binding local port 8899 to receive UDP packag e</li> | |
64 <li>"tcp-listen::8080" – For tcp listening on local port 8080</li> | |
miket_OOO
2012/08/31 22:33:59
TCP
mkearney1
2012/08/31 23:32:12
Done.
| |
65 </ul> | |
66 | |
37 <h2 id="tcp">Using TCP</h2> | 67 <h2 id="tcp">Using TCP</h2> |
38 | 68 |
39 <p> | 69 <p> |
40 Packaged apps can make connections to any service that supports TCP. | 70 Packaged apps can make connections to any service that supports TCP. |
41 </p> | 71 </p> |
42 | 72 |
43 <h3>Connecting to a socket</h3> | 73 <h3>Connecting to a socket</h3> |
44 | 74 |
45 <p> | 75 <p> |
46 Here's a sample showing how to connect to a socket: | 76 Here's a sample showing how to connect to a socket: |
47 </p> | 77 </p> |
48 | 78 |
49 <pre> | 79 <pre> |
50 chrome.socket.create('tcp', {}, function(createInfo) { | 80 chrome.socket.create('tcp', {}, function(createInfo) { |
51 socket.connect(createInfo.socketId, IP, PORT, onConnectedCallback); | 81 socket.connect(createInfo.socketId, IP, PORT, onConnectedCallback); |
52 }); | 82 }); |
53 </pre> | 83 </pre> |
54 | 84 |
55 <p> | 85 <p> |
56 Keep a handle to the socketId so that | 86 Keep a handle to the socketId so that |
57 you can later read and write to this socket. | 87 you can later read and write to this socket. |
58 </p> | 88 </p> |
59 | 89 |
60 <pre> | 90 <pre> |
61 chrome.socket.write(socketId, arrayBuffer, onWriteCompleteCallback); | 91 chrome.socket.write(socketId, arrayBuffer, onWriteCompleteCallback); |
62 </pre> | 92 </pre> |
63 | 93 |
64 <h3>Reading to and writing from a socket</h3> | 94 <h3>Reading to & writing from a socket</h3> |
65 | 95 |
66 <p> | 96 <p> |
67 Reading and writing from a socket uses ArrayBuffer objects. | 97 Reading and writing from a socket uses ArrayBuffer objects. |
68 </p> | 98 </p> |
69 | 99 |
70 <pre> | 100 <pre> |
71 chrome.socket.read(socketId, null, function(readInfo) { | 101 chrome.socket.read(socketId, null, function(readInfo) { |
72 if (readInfo.resultCode > 0) { | 102 if (readInfo.resultCode > 0) { |
73 // readInfo.data is an arrayBuffer. | 103 // readInfo.data is an arrayBuffer. |
74 } | 104 } |
(...skipping 14 matching lines...) Expand all Loading... | |
89 | 119 |
90 <h3>Sending data</h3> | 120 <h3>Sending data</h3> |
91 | 121 |
92 <p> | 122 <p> |
93 Here's a sample showing how to send data | 123 Here's a sample showing how to send data |
94 over the network using UDP: | 124 over the network using UDP: |
95 </p> | 125 </p> |
96 | 126 |
97 <pre> | 127 <pre> |
98 // Create the Socket | 128 // Create the Socket |
99 chrome.experimental.socket.create('udp', '127.0.0.1', 1337, {}, | 129 chrome.socket.create('udp', '127.0.0.1', 1337, {}, |
100 function(socketInfo) { | 130 function(socketInfo) { |
101 // The socket is created, now we want to connect to the service | 131 // The socket is created, now we want to connect to the service |
102 var socketId = socketInfo.socketId; | 132 var socketId = socketInfo.socketId; |
103 chrome.experimental.socket.connect(socketId, function(result) { | 133 chrome.socket.connect(socketId, function(result) { |
104 // We are now connected to the socket so send it some data | 134 // We are now connected to the socket so send it some data |
105 chrome.experimental.socket.write(socketId, arrayBuffer, | 135 chrome.socket.write(socketId, arrayBuffer, |
106 function(sendInfo) { | 136 function(sendInfo) { |
107 console.log("wrote " + sendInfo.bytesWritten); | 137 console.log("wrote " + sendInfo.bytesWritten); |
108 } | 138 } |
109 ); | 139 ); |
110 }); | 140 }); |
111 } | 141 } |
112 ); | 142 ); |
113 </pre> | 143 </pre> |
114 | 144 |
115 <h3>Receiving data</h3> | 145 <h3>Receiving data</h3> |
116 | 146 |
117 <p> | 147 <p> |
118 This example is very similar to the 'Sending data' example | 148 This example is very similar to the 'Sending data' example |
119 with the addition of a special handler in the 'create' method. | 149 with the addition of a special handler in the 'create' method. |
120 The parameter is an object with one value 'onEvent' | 150 The parameter is an object with one value 'onEvent' |
121 that is a function reference to the method | 151 that is a function reference to the method |
122 that will be called when data is available on the port. | 152 that will be called when data is available on the port. |
123 </p> | 153 </p> |
124 | 154 |
125 <pre> | 155 <pre> |
126 // Handle the data response | 156 // Handle the data response |
127 var handleDataEvent = function(d) { | 157 var handleDataEvent = function(d) { |
128 var data = chrome.experimental.socket.read(d.socketId); | 158 var data = chrome.socket.read(d.socketId); |
129 console.log(data); | 159 console.log(data); |
130 }; | 160 }; |
131 | 161 |
132 // Create the Socket | 162 // Create the Socket |
133 chrome.experimental.socket.create('udp', '127.0.0.1', 1337, { onEvent: handleDat aEvent }, | 163 chrome.socket.create('udp', '127.0.0.1', 1337, { onEvent: handleDataEvent }, |
134 function(socketInfo) { | 164 function(socketInfo) { |
135 // The socket is created, now we want to connect to the service | 165 // The socket is created, now we want to connect to the service |
136 var socketId = socketInfo.socketId; | 166 var socketId = socketInfo.socketId; |
137 chrome.experimental.socket.connect(socketId, function(result) { | 167 chrome.socket.connect(socketId, function(result) { |
138 // We are now connected to the socket so send it some data | 168 // We are now connected to the socket so send it some data |
139 chrome.experimental.socket.write(socketId, arrayBuffer, | 169 chrome.socket.write(socketId, arrayBuffer, |
140 function(sendInfo) { | 170 function(sendInfo) { |
141 console.log("wrote " + sendInfo.bytesWritten); | 171 console.log("wrote " + sendInfo.bytesWritten); |
142 } | 172 } |
143 ); | 173 ); |
144 }); | 174 }); |
145 } | 175 } |
146 ); | 176 ); |
147 </pre> | 177 </pre> |
148 | 178 |
149 <p class="backtotop"><a href="#top">Back to top</a></p> | 179 <p class="backtotop"><a href="#top">Back to top</a></p> |
OLD | NEW |