OnMessage event that will never be called - Websockets client (javascript)

I had a problem with a client record to establish a connection to a web socket. The client successfully establishes a connection, but when the server sends data, the OnMessage event never fires. I am using the OWIN Websocket Extension for my part of the server.

Server

dynamic data = fillData(value);
string sendInfo = Newtonsoft.Json.JsonConvert.SerializeObject(data);
byte[] infoBytes = System.Text.Encoding.UTF8.GetBytes(sendInfo);
await sendAsync(new ArraySegment<byte>(infoBytes), 1, false, pair.Key.callCancelled);

Client

    try {
        url = "wss://localhost/piwebapi/elements/E0DqD5loBNH0erqeqJodtALA2U5w0WX-5BGAupiQlq51PAUkVTVFVOSVRcU1VHQVJNQU5cU0FNUExFIEVMRU1FTlQ/attributes";
        socket = new WebSocket(url);

        socket.onopen = function (openEvent) {
            document.getElementById("serverStatus").innerHTML =
               'WebSocket Status:: Socket Open';
            document.getElementById("destination").innerHTML =
                url;
        };

        socket.onmessage = function (messageEvent) {

            if (messageEvent.data instanceof Blob) {
                var destinationCanvas = document.getElementById('destination');
                var destinationContext = destinationCanvas.getContext('2d');
                var image = new Image();
                image.onload = function () {
                    destinationContext.clearRect(0, 0,
                       destinationCanvas.width, destinationCanvas.height);
                    destinationContext.drawImage(image, 0, 0);
                }
                image.src = URL.createObjectURL(messageEvent.data);
            } else {
                document.getElementById("serverResponse").innerHTML =
                   'Server Reply:: ' + messageEvent.data;
            }
        };

        socket.onerror = function (errorEvent) {
            document.getElementById("serverStatus").innerHTML =
              'WebSocket Status:: Error was reported';
        };

        socket.onclose = function (closeEvent) {
            document.getElementById("serverStatus").innerHTML =
              'WebSocket Status:: Socket Closed';
        };
    }
    catch (exception) { if (window.console) console.log(exception); }

OnOpen and OnClose work just fine, but for some reason OnMessage never starts, any ideas?

Change Sending data from the client to the server also works.

+4
source share

All Articles