23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

}<br />

// The protocol here is simple: a four-byte 'network byte order' (big-endian) integer that<br />

// says how long a string is, and then a string that is that long. We wait for exactly 4 bytes,<br />

// read in the count value, and then wait for count bytes, and then display them.<br />

function startServerRead() {<br />

socketsSample.serverReader.loadAsync(4).done(function (sizeBytesRead) {<br />

// Make sure 4 bytes were read.<br />

if (sizeBytesRead !== 4) { /* [Show message] */ }<br />

// Read in the 4 bytes count and then read in that many bytes.<br />

var count = socketsSample.serverReader.readInt32();<br />

return socketsSample.serverReader.loadAsync(count).then(function (stringBytesRead) {<br />

// Make sure the whole string was read.<br />

if (stringBytesRead !== count) { /* [Show message] */ }<br />

// Read in the string.<br />

var string = socketsSample.serverReader.readString(count);<br />

socketsSample.displayOutput("Server read: " + string);<br />

}<br />

// Restart the read for more bytes. We could just call startServerRead() but in<br />

// the case subsequent read operations complete synchronously we start building<br />

// up the stack and potentially crash. We use WinJS.Promise.timeout() to invoke<br />

// this function after the stack for current call unwinds.<br />

WinJS.Promise.timeout().done(function () { return startServerRead(); });<br />

}); // End of "read in rest of string" function.<br />

}, onError);<br />

This code is structured to wait for incoming data that isn’t ready yet, but you might have situations in<br />

which you want to know if there’s more data available that you haven’t read. This value can be obtained<br />

through the DataReader.unconsumedBufferLength property.<br />

In Scenario 2, the data-sending side of the relationship is simple: create a StreamSocket and call<br />

connectAsync (js/connectToListener.js; note that onError uses StreamSocketError.getStatus again):<br />

socketsSample.clientSocket = new Windows.Networking.Sockets.StreamSocket();<br />

socketsSample.clientSocket.connectAsync(hostName, serviceName).done(function () {<br />

// ...<br />

}, onError);<br />

Sending data in Scenario 3 takes advantage of a DataWriter built on the socket’s output stream<br />

(js/sendData.js):<br />

var writer = new Windows.Storage.Streams.DataWriter(socketsSample.clientSocket.outputStream);<br />

var string = "Hello World";<br />

var len = writer.measureString(string); // Gets the UTF-8 string length.<br />

writer.writeInt32(len);<br />

writer.writeString(string);<br />

writer.storeAsync().done(function () {<br />

writer.detachStream();<br />

}, onError);<br />

682

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!