I ran into this alongside the findAllAsync trouble I was having in the previous post. To be more specific, I've been converting a small sample that's in the ZIP file for //build 2013 session 3-026 (http://channel9.msdn.com/Events/Build/2013/3-026, the ZIP is http://files.channel9.msdn.com/thumbnail/f9025b07-120c-4320-8de6-a5de2d255b7c.zip) . This is a C# sample to talk to a Sphero device using the Windows 8.1 Bluetooth.Rfcomm API.

The problem I was having was that write.storeAsync (write is a Windows.Storage.Streams.DataWriter object) was throwing an exception, hence my try/catch block:

function changeColor() {
    output.innerText = "";

    if (writer == null) {
        return;
    }

    var packet = generateRandomColorPacket();
    writer.writeBytes(packet);

    try { 
        writer.storeAsync().done(function () {
        }, function (e) {
            output.innerText = "Failed to write packet; " + e;
        });
    } catch (e) {
        output.innerText = "DataWriter.storeAsync failed (exception); " + e;
    }
}

The exception message wasn't at all helpful: it just said: "The operation identifier is not valid." Huh?

With the help of some colleagues in the Bluetooth team who looked this over, we found a small glitch in the code below that opens the socket to the device. Do you see the error?

function openSocket() {
    output.innerText = "";

    if (device == null) {
        return;
    }

    // Create a socket and connect to the target
    var sockNS = Windows.Networking.Sockets;
    socket = new sockNS.StreamSocket();
    socket.connectAsync(device.connectionHostName, device.connectionServiceName,
        sockNS.SocketProtectionLevel.bluetoothEncryptionAllowNullAuthentication)
        .done(function () {
            writer = new Windows.Storage.Streams.DataWriter(socket.OutputStream);
        }, function (e) {
            output.innerText = "Failed to open socket; " + e;
        });
}

Don't feel bad if you missed it, because I did. It's this line here, which I'd brought over from the C# code. Now do you see the problem?

writer = new Windows.Storage.Streams.DataWriter(socket.OutputStream);

It's hard to see: socket.OutputStream should be socket.outputStream! In other words, I was creating a DataWriter with undefined rather than the real stream. DataWriter didn't complain, though, until it tried to access the backing stream via storeAsync.

Changing that one character solved the problem.


In contrast to other posts in this series of HTML5/WinRT comparisons, this one on web sockets is relatively short. I haven’t worked too much with either set of APIs, so only have a few details to write up.

Standard WebSockets, as they’re defined in the W3C/HTML5 API, are entirely supported for Windows Store apps. However, they support only a transaction-based UDP model like DatagramSocket and only text content. The MessageWebSocket in WinRT supports both text and binary, plus you can use the StreamWebSocket for a streaming TCP model as well. The WinRT APIs also emit more detailed error information.

For all these reasons, we generally recommend using the WinRT APIs for web sockets over the HTML5 APIs.