OLD | NEW |
(Empty) | |
| 1 <h1>Network Communications</h1> |
| 2 |
| 3 |
| 4 <p> |
| 5 Packaged apps can act as a network client |
| 6 for TCP and UDP connections. |
| 7 This doc shows you how to use TCP and UDP |
| 8 to send and receive data over the network. |
| 9 For more information, |
| 10 see the |
| 11 <a href="experimental.socket.html">Sockets API</a>. |
| 12 </p> |
| 13 |
| 14 <p class="note"> |
| 15 <b>API Samples: </b> |
| 16 Want to play with the code? |
| 17 Check out the |
| 18 <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. |
| 20 </p> |
| 21 |
| 22 <h2 id="manifest">Manifest requirements</h2> |
| 23 |
| 24 <p> |
| 25 For packaged apps that use TCP or UDP, |
| 26 add the "experimental" and "socket" permissions |
| 27 to the manifest: |
| 28 </p> |
| 29 |
| 30 <pre> |
| 31 "permissions": [ |
| 32 "experimental", |
| 33 "socket" |
| 34 ] |
| 35 </pre> |
| 36 |
| 37 <h2 id="tcp">Using TCP</h2> |
| 38 |
| 39 <p> |
| 40 Packaged apps can make connections to any service that supports TCP. |
| 41 </p> |
| 42 |
| 43 <h3>Connecting to a socket</h3> |
| 44 |
| 45 <p> |
| 46 Here's a sample showing how to connect to a socket: |
| 47 </p> |
| 48 |
| 49 <pre> |
| 50 chrome.socket.create('tcp', {}, function(createInfo) { |
| 51 socket.connect(createInfo.socketId, IP, PORT, onConnectedCallback); |
| 52 }); |
| 53 </pre> |
| 54 |
| 55 <p> |
| 56 Keep a handle to the socketId so that |
| 57 you can later read and write to this socket. |
| 58 </p> |
| 59 |
| 60 <pre> |
| 61 chrome.socket.write(socketId, arrayBuffer, onWriteCompleteCallback); |
| 62 </pre> |
| 63 |
| 64 <h3>Reading to and writing from a socket</h3> |
| 65 |
| 66 <p> |
| 67 Reading and writing from a socket uses ArrayBuffer objects. |
| 68 </p> |
| 69 |
| 70 <pre> |
| 71 chrome.socket.read(socketId, null, function(readInfo) { |
| 72 if (readInfo.resultCode > 0) { |
| 73 // readInfo.data is an arrayBuffer. |
| 74 } |
| 75 }); |
| 76 </pre> |
| 77 |
| 78 <h3>Disconnecting from a socket</h3> |
| 79 |
| 80 <p>Here's how to disconnect:</p> |
| 81 |
| 82 <pre>chrome.socket.disconnect(socketId);</pre> |
| 83 |
| 84 <h2 id="udp">Using UDP</h2> |
| 85 |
| 86 <p> |
| 87 Packaged apps can make connections to any service that supports UDP. |
| 88 </p> |
| 89 |
| 90 <h3>Sending data</h3> |
| 91 |
| 92 <p> |
| 93 Here's a sample showing how to send data |
| 94 over the network using UDP: |
| 95 </p> |
| 96 |
| 97 <pre> |
| 98 // Create the Socket |
| 99 chrome.experimental.socket.create('udp', '127.0.0.1', 1337, {}, |
| 100 function(socketInfo) { |
| 101 // The socket is created, now we want to connect to the service |
| 102 var socketId = socketInfo.socketId; |
| 103 chrome.experimental.socket.connect(socketId, function(result) { |
| 104 // We are now connected to the socket so send it some data |
| 105 chrome.experimental.socket.write(socketId, arrayBuffer, |
| 106 function(sendInfo) { |
| 107 console.log("wrote " + sendInfo.bytesWritten); |
| 108 } |
| 109 ); |
| 110 }); |
| 111 } |
| 112 ); |
| 113 </pre> |
| 114 |
| 115 <h3>Receiving data</h3> |
| 116 |
| 117 <p> |
| 118 This example is very similar to the 'Sending data' example |
| 119 with the addition of a special handler in the 'create' method. |
| 120 The parameter is an object with one value 'onEvent' |
| 121 that is a function reference to the method |
| 122 that will be called when data is available on the port. |
| 123 </p> |
| 124 |
| 125 <pre> |
| 126 // Handle the data response |
| 127 var handleDataEvent = function(d) { |
| 128 var data = chrome.experimental.socket.read(d.socketId); |
| 129 console.log(data); |
| 130 }; |
| 131 |
| 132 // Create the Socket |
| 133 chrome.experimental.socket.create('udp', '127.0.0.1', 1337, { onEvent: handleDat
aEvent }, |
| 134 function(socketInfo) { |
| 135 // The socket is created, now we want to connect to the service |
| 136 var socketId = socketInfo.socketId; |
| 137 chrome.experimental.socket.connect(socketId, function(result) { |
| 138 // We are now connected to the socket so send it some data |
| 139 chrome.experimental.socket.write(socketId, arrayBuffer, |
| 140 function(sendInfo) { |
| 141 console.log("wrote " + sendInfo.bytesWritten); |
| 142 } |
| 143 ); |
| 144 }); |
| 145 } |
| 146 ); |
| 147 </pre> |
| 148 |
| 149 <p class="backtotop"><a href="#top">Back to top</a></p> |
OLD | NEW |