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