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