OLD | NEW |
1 <h1>Network Communications</h1> | 1 <h1>Network Communications</h1> |
2 | 2 |
3 <p> | 3 <p> |
4 Packaged apps can act as a network client | 4 Packaged apps can act as a network client |
5 for TCP and UDP connections. | 5 for TCP and UDP connections. |
6 This doc shows you how to use TCP and UDP | 6 This doc shows you how to use TCP and UDP |
7 to send and receive data over the network. | 7 to send and receive data over the network. |
8 For more information, | 8 For more information, |
9 see the | 9 see the |
10 <a href="socket.html">Sockets API</a>. | 10 <a href="socket.html">Sockets API</a>. |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 <li>"udp-bind::8899" – binding local port 8899 to receive UDP package</l
i> | 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> | 63 <li>"tcp-listen::8080" – TCP listening on local port 8080</li> |
64 </ul> | 64 </ul> |
65 | 65 |
66 <h2 id="tcp">Using TCP</h2> | 66 <h2 id="tcp">Using TCP</h2> |
67 | 67 |
68 <p> | 68 <p> |
69 Packaged apps can make connections to any service that supports TCP. | 69 Packaged apps can make connections to any service that supports TCP. |
70 </p> | 70 </p> |
71 | 71 |
72 <h3>Connecting to a socket</h3> | 72 <h3 id="connecting">Connecting to a socket</h3> |
73 | 73 |
74 <p> | 74 <p> |
75 Here's a sample showing how to connect to a socket: | 75 Here's a sample showing how to connect to a socket: |
76 </p> | 76 </p> |
77 | 77 |
78 <pre> | 78 <pre> |
79 chrome.socket.create('tcp', {}, function(createInfo) { | 79 chrome.socket.create('tcp', {}, function(createInfo) { |
80 socket.connect(createInfo.socketId, IP, PORT, onConnectedCallback); | 80 socket.connect(createInfo.socketId, IP, PORT, onConnectedCallback); |
81 }); | 81 }); |
82 </pre> | 82 </pre> |
83 | 83 |
84 <p> | 84 <p> |
85 Keep a handle to the socketId so that | 85 Keep a handle to the socketId so that |
86 you can later read and write to this socket. | 86 you can later read and write to this socket. |
87 </p> | 87 </p> |
88 | 88 |
89 <pre> | 89 <pre> |
90 chrome.socket.write(socketId, arrayBuffer, onWriteCompleteCallback); | 90 chrome.socket.write(socketId, arrayBuffer, onWriteCompleteCallback); |
91 </pre> | 91 </pre> |
92 | 92 |
93 <h3>Reading to & writing from a socket</h3> | 93 <h3 id="reading">Reading to & writing from a socket</h3> |
94 | 94 |
95 <p> | 95 <p> |
96 Reading and writing from a socket uses ArrayBuffer objects. | 96 Reading and writing from a socket uses ArrayBuffer objects. |
97 </p> | 97 </p> |
98 | 98 |
99 <pre> | 99 <pre> |
100 chrome.socket.read(socketId, null, function(readInfo) { | 100 chrome.socket.read(socketId, null, function(readInfo) { |
101 if (readInfo.resultCode > 0) { | 101 if (readInfo.resultCode > 0) { |
102 // readInfo.data is an arrayBuffer. | 102 // readInfo.data is an arrayBuffer. |
103 } | 103 } |
104 }); | 104 }); |
105 </pre> | 105 </pre> |
106 | 106 |
107 <h3>Disconnecting from a socket</h3> | 107 <h3 id="disconnecting">Disconnecting from a socket</h3> |
108 | 108 |
109 <p>Here's how to disconnect:</p> | 109 <p>Here's how to disconnect:</p> |
110 | 110 |
111 <pre>chrome.socket.disconnect(socketId);</pre> | 111 <pre>chrome.socket.disconnect(socketId);</pre> |
112 | 112 |
113 <h2 id="udp">Using UDP</h2> | 113 <h2 id="udp">Using UDP</h2> |
114 | 114 |
115 <p> | 115 <p> |
116 Packaged apps can make connections to any service that supports UDP. | 116 Packaged apps can make connections to any service that supports UDP. |
117 </p> | 117 </p> |
118 | 118 |
119 <h3>Sending data</h3> | 119 <h3 id="sending">Sending data</h3> |
120 | 120 |
121 <p> | 121 <p> |
122 Here's a sample showing how to send data | 122 Here's a sample showing how to send data |
123 over the network using UDP: | 123 over the network using UDP: |
124 </p> | 124 </p> |
125 | 125 |
126 <pre> | 126 <pre> |
127 // Create the Socket | 127 // Create the Socket |
128 chrome.socket.create('udp', '127.0.0.1', 1337, {}, | 128 chrome.socket.create('udp', '127.0.0.1', 1337, {}, |
129 function(socketInfo) { | 129 function(socketInfo) { |
130 // 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 |
131 var socketId = socketInfo.socketId; | 131 var socketId = socketInfo.socketId; |
132 chrome.socket.connect(socketId, function(result) { | 132 chrome.socket.connect(socketId, function(result) { |
133 // 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 |
134 chrome.socket.write(socketId, arrayBuffer, | 134 chrome.socket.write(socketId, arrayBuffer, |
135 function(sendInfo) { | 135 function(sendInfo) { |
136 console.log("wrote " + sendInfo.bytesWritten); | 136 console.log("wrote " + sendInfo.bytesWritten); |
137 } | 137 } |
138 ); | 138 ); |
139 }); | 139 }); |
140 } | 140 } |
141 ); | 141 ); |
142 </pre> | 142 </pre> |
143 | 143 |
144 <h3>Receiving data</h3> | 144 <h3 id="receiving">Receiving data</h3> |
145 | 145 |
146 <p> | 146 <p> |
147 This example is very similar to the 'Sending data' example | 147 This example is very similar to the 'Sending data' example |
148 with the addition of a special handler in the 'create' method. | 148 with the addition of a special handler in the 'create' method. |
149 The parameter is an object with one value 'onEvent' | 149 The parameter is an object with one value 'onEvent' |
150 that is a function reference to the method | 150 that is a function reference to the method |
151 that will be called when data is available on the port. | 151 that will be called when data is available on the port. |
152 </p> | 152 </p> |
153 | 153 |
154 <pre> | 154 <pre> |
(...skipping 13 matching lines...) Expand all Loading... |
168 chrome.socket.write(socketId, arrayBuffer, | 168 chrome.socket.write(socketId, arrayBuffer, |
169 function(sendInfo) { | 169 function(sendInfo) { |
170 console.log("wrote " + sendInfo.bytesWritten); | 170 console.log("wrote " + sendInfo.bytesWritten); |
171 } | 171 } |
172 ); | 172 ); |
173 }); | 173 }); |
174 } | 174 } |
175 ); | 175 ); |
176 </pre> | 176 </pre> |
177 | 177 |
178 <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 |