Chromium Code Reviews| 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>. |
|
battre
2012/09/03 13:35:37
Socket API (without s)
| |
| 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 '*'>+ | |
|
battre
2012/09/03 13:35:37
Is this precise? www.example.com would not comply
| |
| 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" – connecting on port 23 of any hosts</li> | |
| 59 <li>"tcp-connect:www.example.com:23" – connecting port 23 of <em>www.exa mple.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</l i> | |
| 63 <li>"tcp-listen::8080" – TCP listening on local port 8080</li> | |
| 64 </ul> | |
| 65 | |
| 37 <h2 id="tcp">Using TCP</h2> | 66 <h2 id="tcp">Using TCP</h2> |
| 38 | 67 |
| 39 <p> | 68 <p> |
| 40 Packaged apps can make connections to any service that supports TCP. | 69 Packaged apps can make connections to any service that supports TCP. |
| 41 </p> | 70 </p> |
| 42 | 71 |
| 43 <h3>Connecting to a socket</h3> | 72 <h3>Connecting to a socket</h3> |
| 44 | 73 |
| 45 <p> | 74 <p> |
| 46 Here's a sample showing how to connect to a socket: | 75 Here's a sample showing how to connect to a socket: |
| 47 </p> | 76 </p> |
| 48 | 77 |
| 49 <pre> | 78 <pre> |
| 50 chrome.socket.create('tcp', {}, function(createInfo) { | 79 chrome.socket.create('tcp', {}, function(createInfo) { |
| 51 socket.connect(createInfo.socketId, IP, PORT, onConnectedCallback); | 80 socket.connect(createInfo.socketId, IP, PORT, onConnectedCallback); |
| 52 }); | 81 }); |
| 53 </pre> | 82 </pre> |
|
battre
2012/09/03 13:35:37
Add an example how you check for errors in onConne
| |
| 54 | 83 |
| 55 <p> | 84 <p> |
| 56 Keep a handle to the socketId so that | 85 Keep a handle to the socketId so that |
| 57 you can later read and write to this socket. | 86 you can later read and write to this socket. |
| 58 </p> | 87 </p> |
| 59 | 88 |
| 60 <pre> | 89 <pre> |
| 61 chrome.socket.write(socketId, arrayBuffer, onWriteCompleteCallback); | 90 chrome.socket.write(socketId, arrayBuffer, onWriteCompleteCallback); |
| 62 </pre> | 91 </pre> |
| 63 | 92 |
| 64 <h3>Reading to and writing from a socket</h3> | 93 <h3>Reading to & writing from a socket</h3> |
| 65 | 94 |
| 66 <p> | 95 <p> |
| 67 Reading and writing from a socket uses ArrayBuffer objects. | 96 Reading and writing from a socket uses ArrayBuffer objects. |
| 68 </p> | 97 </p> |
| 69 | 98 |
| 70 <pre> | 99 <pre> |
| 71 chrome.socket.read(socketId, null, function(readInfo) { | 100 chrome.socket.read(socketId, null, function(readInfo) { |
| 72 if (readInfo.resultCode > 0) { | 101 if (readInfo.resultCode > 0) { |
| 73 // readInfo.data is an arrayBuffer. | 102 // readInfo.data is an arrayBuffer. |
| 74 } | 103 } |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 89 | 118 |
| 90 <h3>Sending data</h3> | 119 <h3>Sending data</h3> |
| 91 | 120 |
| 92 <p> | 121 <p> |
| 93 Here's a sample showing how to send data | 122 Here's a sample showing how to send data |
| 94 over the network using UDP: | 123 over the network using UDP: |
| 95 </p> | 124 </p> |
| 96 | 125 |
| 97 <pre> | 126 <pre> |
| 98 // Create the Socket | 127 // Create the Socket |
| 99 chrome.experimental.socket.create('udp', '127.0.0.1', 1337, {}, | 128 chrome.socket.create('udp', '127.0.0.1', 1337, {}, |
|
battre
2012/09/03 13:35:37
Does this open a socket for incoming traffic *from
battre
2012/09/03 13:35:37
This is incorrect. create does not take the IP add
battre
2012/09/03 13:35:37
socket.create() takes a CreateOptions parameter bu
| |
| 100 function(socketInfo) { | 129 function(socketInfo) { |
|
battre
2012/09/03 13:35:37
Error handling?
| |
| 101 // The socket is created, now we want to connect to the service | 130 // The socket is created, now we want to connect to the service |
| 102 var socketId = socketInfo.socketId; | 131 var socketId = socketInfo.socketId; |
| 103 chrome.experimental.socket.connect(socketId, function(result) { | 132 chrome.socket.connect(socketId, function(result) { |
|
battre
2012/09/03 13:35:37
This is wrong: connect takes an IP address and por
| |
| 104 // We are now connected to the socket so send it some data | 133 // We are now connected to the socket so send it some data |
| 105 chrome.experimental.socket.write(socketId, arrayBuffer, | 134 chrome.socket.write(socketId, arrayBuffer, |
| 106 function(sendInfo) { | 135 function(sendInfo) { |
| 107 console.log("wrote " + sendInfo.bytesWritten); | 136 console.log("wrote " + sendInfo.bytesWritten); |
| 108 } | 137 } |
| 109 ); | 138 ); |
| 110 }); | 139 }); |
| 111 } | 140 } |
| 112 ); | 141 ); |
| 113 </pre> | 142 </pre> |
| 114 | 143 |
| 115 <h3>Receiving data</h3> | 144 <h3>Receiving data</h3> |
| 116 | 145 |
| 117 <p> | 146 <p> |
| 118 This example is very similar to the 'Sending data' example | 147 This example is very similar to the 'Sending data' example |
| 119 with the addition of a special handler in the 'create' method. | 148 with the addition of a special handler in the 'create' method. |
| 120 The parameter is an object with one value 'onEvent' | 149 The parameter is an object with one value 'onEvent' |
| 121 that is a function reference to the method | 150 that is a function reference to the method |
| 122 that will be called when data is available on the port. | 151 that will be called when data is available on the port. |
| 123 </p> | 152 </p> |
| 124 | 153 |
| 125 <pre> | 154 <pre> |
| 126 // Handle the data response | 155 // Handle the data response |
| 127 var handleDataEvent = function(d) { | 156 var handleDataEvent = function(d) { |
| 128 var data = chrome.experimental.socket.read(d.socketId); | 157 var data = chrome.socket.read(d.socketId); |
|
battre
2012/09/03 13:35:37
This is incorrect: Read uses a callback function.
| |
| 129 console.log(data); | 158 console.log(data); |
| 130 }; | 159 }; |
| 131 | 160 |
| 132 // Create the Socket | 161 // Create the Socket |
| 133 chrome.experimental.socket.create('udp', '127.0.0.1', 1337, { onEvent: handleDat aEvent }, | 162 chrome.socket.create('udp', '127.0.0.1', 1337, { onEvent: handleDataEvent }, |
|
battre
2012/09/03 13:35:37
This is incorrect. create does not take the IP add
battre
2012/09/03 13:35:37
Don't you need to bind() the socket for incoming d
| |
| 134 function(socketInfo) { | 163 function(socketInfo) { |
| 135 // The socket is created, now we want to connect to the service | 164 // The socket is created, now we want to connect to the service |
| 136 var socketId = socketInfo.socketId; | 165 var socketId = socketInfo.socketId; |
| 137 chrome.experimental.socket.connect(socketId, function(result) { | 166 chrome.socket.connect(socketId, function(result) { |
| 138 // We are now connected to the socket so send it some data | 167 // We are now connected to the socket so send it some data |
| 139 chrome.experimental.socket.write(socketId, arrayBuffer, | 168 chrome.socket.write(socketId, arrayBuffer, |
| 140 function(sendInfo) { | 169 function(sendInfo) { |
| 141 console.log("wrote " + sendInfo.bytesWritten); | 170 console.log("wrote " + sendInfo.bytesWritten); |
| 142 } | 171 } |
| 143 ); | 172 ); |
| 144 }); | 173 }); |
| 145 } | 174 } |
| 146 ); | 175 ); |
| 147 </pre> | 176 </pre> |
| 148 | 177 |
| 149 <p class="backtotop"><a href="#top">Back to top</a></p> | 178 <p class="backtotop"><a href="#top">Back to top</a></p> |
| OLD | NEW |