最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - WebSocket not sending data - Stack Overflow

programmeradmin0浏览0评论

I've got a bit of a problem with my websocket code intermittently not sending data... As it stands I've written a small C# server which handles the websocket and prints a label.

When running the code on my puter it works fine every time but when I tried it on two other puters I had problems.

I set up some packet sniffing software and what I seem to see as follows:

When the application works... The browser creates a connection to the server which pletes the hand shake. Once pleted the websocket.onopen event fires. During this event I send data to the server (which I can see in Network Monitor) and then check for buffered data before closing the websocket.

When the application doesn't work... Exactly the same thing happens, however I can't see the data in Network Monitor. The handshake is there, and the onopen event fires and closes the connection but no data is sent over the network...

I installed Network Monitor on both the client and server machine and neither machine can see the missing the data.

Can anyone see any problems in my code below? Failing that does anyone know if this is a bug?

The code I wrote is as follows:

var _socket;
function sendXmlToLabelPrinter(strXML)
{
if (_socket == null)
{
console.log("creating socket");
_socket = new WebSocket("ws://127.0.0.1:50/"); // LOCAL
_socket.binaryType = "arraybuffer";

_socket.onopen = function()
{
    console.log("socket opened");

    if (_socket.readyState == WebSocket.OPEN)
    {
        try
        {
            _socket.send(strXML);
        }
        catch (ex)
        {
            console.log("an error occured when sending");
            console.log(ex);
        }

        var intClose = setInterval
        ( 
            function()
            {
                console.log("checking socket buffer");
                if (_socket.bufferedAmount == 0)
                {
                    console.log("closing socket");
                    _socket.close();
                    _socket = null;
                    clearTimeout(intClose);
                }
            },
            250
        );
        hideUserMessage();
    }
    else
    {
        console.log("socket not ready");    
    }
};

_socket.onerror = function(myErr)
{
    console.log("error connecting to label printer, set timer to try again");
    _socket.close();
    _socket = null;

    showUserMessage("Error: Please check label printer is running (Retrying in 5s)");
    setTimeout(function(){hideUserMessage()}, 2000);
    setTimeout(function(){sendXmlToLabelPrinter(strXML)}, 5000);
};
}
else
{
    console.log("socket is in use, set timer to retry");
    setTimeout(function(){sendXmlToLabelPrinter(strXML)}, 250);
}

}

I've got a bit of a problem with my websocket code intermittently not sending data... As it stands I've written a small C# server which handles the websocket and prints a label.

When running the code on my puter it works fine every time but when I tried it on two other puters I had problems.

I set up some packet sniffing software and what I seem to see as follows:

When the application works... The browser creates a connection to the server which pletes the hand shake. Once pleted the websocket.onopen event fires. During this event I send data to the server (which I can see in Network Monitor) and then check for buffered data before closing the websocket.

When the application doesn't work... Exactly the same thing happens, however I can't see the data in Network Monitor. The handshake is there, and the onopen event fires and closes the connection but no data is sent over the network...

I installed Network Monitor on both the client and server machine and neither machine can see the missing the data.

Can anyone see any problems in my code below? Failing that does anyone know if this is a bug?

The code I wrote is as follows:

var _socket;
function sendXmlToLabelPrinter(strXML)
{
if (_socket == null)
{
console.log("creating socket");
_socket = new WebSocket("ws://127.0.0.1:50/"); // LOCAL
_socket.binaryType = "arraybuffer";

_socket.onopen = function()
{
    console.log("socket opened");

    if (_socket.readyState == WebSocket.OPEN)
    {
        try
        {
            _socket.send(strXML);
        }
        catch (ex)
        {
            console.log("an error occured when sending");
            console.log(ex);
        }

        var intClose = setInterval
        ( 
            function()
            {
                console.log("checking socket buffer");
                if (_socket.bufferedAmount == 0)
                {
                    console.log("closing socket");
                    _socket.close();
                    _socket = null;
                    clearTimeout(intClose);
                }
            },
            250
        );
        hideUserMessage();
    }
    else
    {
        console.log("socket not ready");    
    }
};

_socket.onerror = function(myErr)
{
    console.log("error connecting to label printer, set timer to try again");
    _socket.close();
    _socket = null;

    showUserMessage("Error: Please check label printer is running (Retrying in 5s)");
    setTimeout(function(){hideUserMessage()}, 2000);
    setTimeout(function(){sendXmlToLabelPrinter(strXML)}, 5000);
};
}
else
{
    console.log("socket is in use, set timer to retry");
    setTimeout(function(){sendXmlToLabelPrinter(strXML)}, 250);
}

}

Share Improve this question edited Jan 17, 2014 at 17:03 rene 42.5k78 gold badges121 silver badges165 bronze badges asked Jan 17, 2014 at 17:01 DanielDaniel 831 gold badge2 silver badges9 bronze badges 4
  • On your two other puters are you changing the websocket URL to point to the C# server's address? 127.0.0.1 is localhost, you should look at what your server's actual IP address is and use that address in your script instead. – Arjun Mehta Commented Jan 17, 2014 at 17:25
  • Hi guys. Thanks for the suggestions. The IP address is as intended. The web application prints to a local printer via the mini server application i wrote. I've tried running the application both using local host and across puters (as network monitor can't sniff local packets) and the issue remains... It could be a network issue somewhere inside of windows but it's not caused by the IP address Im using. Cheers – Daniel Commented Jan 17, 2014 at 23:16
  • I'm not sure I understand your set up then! Is there one puter running the C# server and all other puters trying to connect to that? Or is there a c# server running on each machine? – Arjun Mehta Commented Jan 17, 2014 at 23:22
  • Sorry for the slow reply. There's one C# server running on each puter. The websocket connects back to the server on 127.0.0.1. – Daniel Commented Jan 20, 2014 at 11:21
Add a ment  | 

3 Answers 3

Reset to default 2

You might want to make sure the TrendMicro or other virus scanners are not interrupting the WebSocket network traffic. This exact behavior (no packets after handshake) kept a couple of engineers awake at night a couple of years ago with WebSockets on Windows.

A guess: This is a networking issue, and has to do with the address you're using for your websocket.

Your Websocket URL is pointing to 127.0.0.1, (ie. localhost), which might explain why it works on the puter that is running the server.

_socket = new WebSocket("ws://127.0.0.1:50/");

Should be changed to:

_socket = new WebSocket("ws://yourServer'sPublicIP:50/");

You might need a network utility to identify what your server's IP is on the network. Hope this helps!

As Nowucca suggested the anti virus software was the culprit. Sophos Web Intelligence intermittently stops Chrome from being able to send data over a websocket. Chrome unfortunately can't detect this and thinks that the data has actually been sent. I confirmed this my trying repeatedly with the service on and off on multiple puters. Hope this helps someone in the future. Cheers.

发布评论

评论列表(0)

  1. 暂无评论