OLD | NEW |
1 <h1>Network Communications</h1> | 1 <h1>Network Communications</h1> |
2 | 2 |
3 | 3 |
4 <p> | 4 <p> |
5 Packaged apps can act as a network client | 5 Packaged apps can act as a network client |
6 for TCP and UDP connections. | 6 for TCP and UDP connections. |
7 This doc shows you how to use TCP and UDP | 7 This doc shows you how to use TCP and UDP |
8 to send and receive data over the network. | 8 to send and receive data over the network. |
9 For more information, | 9 For more information, |
10 see the | 10 see the |
(...skipping 22 matching lines...) Expand all Loading... |
33 "socket" | 33 "socket" |
34 ] | 34 ] |
35 </pre> | 35 </pre> |
36 | 36 |
37 <h2 id="tcp">Using TCP</h2> | 37 <h2 id="tcp">Using TCP</h2> |
38 | 38 |
39 <p> | 39 <p> |
40 Packaged apps can make connections to any service that supports TCP. | 40 Packaged apps can make connections to any service that supports TCP. |
41 </p> | 41 </p> |
42 | 42 |
43 <h3>Connecting to a socket</h3> | 43 <h3 id="connecting">Connecting to a socket</h3> |
44 | 44 |
45 <p> | 45 <p> |
46 Here's a sample showing how to connect to a socket: | 46 Here's a sample showing how to connect to a socket: |
47 </p> | 47 </p> |
48 | 48 |
49 <pre> | 49 <pre> |
50 chrome.socket.create('tcp', {}, function(createInfo) { | 50 chrome.socket.create('tcp', {}, function(createInfo) { |
51 socket.connect(createInfo.socketId, IP, PORT, onConnectedCallback); | 51 socket.connect(createInfo.socketId, IP, PORT, onConnectedCallback); |
52 }); | 52 }); |
53 </pre> | 53 </pre> |
54 | 54 |
55 <p> | 55 <p> |
56 Keep a handle to the socketId so that | 56 Keep a handle to the socketId so that |
57 you can later read and write to this socket. | 57 you can later read and write to this socket. |
58 </p> | 58 </p> |
59 | 59 |
60 <pre> | 60 <pre> |
61 chrome.socket.write(socketId, arrayBuffer, onWriteCompleteCallback); | 61 chrome.socket.write(socketId, arrayBuffer, onWriteCompleteCallback); |
62 </pre> | 62 </pre> |
63 | 63 |
64 <h3>Reading to and writing from a socket</h3> | 64 <h3 id="reading">Reading to & writing from a socket</h3> |
65 | 65 |
66 <p> | 66 <p> |
67 Reading and writing from a socket uses ArrayBuffer objects. | 67 Reading and writing from a socket uses ArrayBuffer objects. |
68 </p> | 68 </p> |
69 | 69 |
70 <pre> | 70 <pre> |
71 chrome.socket.read(socketId, null, function(readInfo) { | 71 chrome.socket.read(socketId, null, function(readInfo) { |
72 if (readInfo.resultCode > 0) { | 72 if (readInfo.resultCode > 0) { |
73 // readInfo.data is an arrayBuffer. | 73 // readInfo.data is an arrayBuffer. |
74 } | 74 } |
75 }); | 75 }); |
76 </pre> | 76 </pre> |
77 | 77 |
78 <h3>Disconnecting from a socket</h3> | 78 <h3 id="disconnecting">Disconnecting from a socket</h3> |
79 | 79 |
80 <p>Here's how to disconnect:</p> | 80 <p>Here's how to disconnect:</p> |
81 | 81 |
82 <pre>chrome.socket.disconnect(socketId);</pre> | 82 <pre>chrome.socket.disconnect(socketId);</pre> |
83 | 83 |
84 <h2 id="udp">Using UDP</h2> | 84 <h2 id="udp">Using UDP</h2> |
85 | 85 |
86 <p> | 86 <p> |
87 Packaged apps can make connections to any service that supports UDP. | 87 Packaged apps can make connections to any service that supports UDP. |
88 </p> | 88 </p> |
89 | 89 |
90 <h3>Sending data</h3> | 90 <h3 id="sending">Sending data</h3> |
91 | 91 |
92 <p> | 92 <p> |
93 Here's a sample showing how to send data | 93 Here's a sample showing how to send data |
94 over the network using UDP: | 94 over the network using UDP: |
95 </p> | 95 </p> |
96 | 96 |
97 <pre> | 97 <pre> |
98 // Create the Socket | 98 // Create the Socket |
99 chrome.experimental.socket.create('udp', '127.0.0.1', 1337, {}, | 99 chrome.experimental.socket.create('udp', '127.0.0.1', 1337, {}, |
100 function(socketInfo) { | 100 function(socketInfo) { |
101 // The socket is created, now we want to connect to the service | 101 // The socket is created, now we want to connect to the service |
102 var socketId = socketInfo.socketId; | 102 var socketId = socketInfo.socketId; |
103 chrome.experimental.socket.connect(socketId, function(result) { | 103 chrome.experimental.socket.connect(socketId, function(result) { |
104 // We are now connected to the socket so send it some data | 104 // We are now connected to the socket so send it some data |
105 chrome.experimental.socket.write(socketId, arrayBuffer, | 105 chrome.experimental.socket.write(socketId, arrayBuffer, |
106 function(sendInfo) { | 106 function(sendInfo) { |
107 console.log("wrote " + sendInfo.bytesWritten); | 107 console.log("wrote " + sendInfo.bytesWritten); |
108 } | 108 } |
109 ); | 109 ); |
110 }); | 110 }); |
111 } | 111 } |
112 ); | 112 ); |
113 </pre> | 113 </pre> |
114 | 114 |
115 <h3>Receiving data</h3> | 115 <h3 id="receiving">Receiving data</h3> |
116 | 116 |
117 <p> | 117 <p> |
118 This example is very similar to the 'Sending data' example | 118 This example is very similar to the 'Sending data' example |
119 with the addition of a special handler in the 'create' method. | 119 with the addition of a special handler in the 'create' method. |
120 The parameter is an object with one value 'onEvent' | 120 The parameter is an object with one value 'onEvent' |
121 that is a function reference to the method | 121 that is a function reference to the method |
122 that will be called when data is available on the port. | 122 that will be called when data is available on the port. |
123 </p> | 123 </p> |
124 | 124 |
125 <pre> | 125 <pre> |
(...skipping 13 matching lines...) Expand all Loading... |
139 chrome.experimental.socket.write(socketId, arrayBuffer, | 139 chrome.experimental.socket.write(socketId, arrayBuffer, |
140 function(sendInfo) { | 140 function(sendInfo) { |
141 console.log("wrote " + sendInfo.bytesWritten); | 141 console.log("wrote " + sendInfo.bytesWritten); |
142 } | 142 } |
143 ); | 143 ); |
144 }); | 144 }); |
145 } | 145 } |
146 ); | 146 ); |
147 </pre> | 147 </pre> |
148 | 148 |
149 <p class="backtotop"><a href="#top">Back to top</a></p> | 149 <p class="backtotop"><a href="#top">Back to top</a></p> |
OLD | NEW |