1

Using the code from here for testing. The javascript runs just fine in Google Chrome and Firefox but not in IE.

Ok so it works in IE if I run the html file locally but it does give me a warning at the bottom of the page saying it has disabled some scripts on the page and I have to click it to enable them.(doesnt happen in chrome) but ok I do that and it works. But I then put it on my remote webserver which just hosts the HTML file. The connection is still going to be local to the browser viewing the site. However this wont work in IE but could be because it doesn't even show a warning about the script. The page loads but the js doesn't run.

Im assuming that IE is blocking it by default.

How can I get round this and make it work like it does in chrome. The html/js is below.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script type="text/javascript">
    function connect() {
        var ws = new WebSocket("ws://localhost:8080/service");
        ws.onopen = function () {
            alert("About to send data");
            ws.send("Hello World"); // I WANT TO SEND THIS MESSAGE TO THE SERVER!!!!!!!!
            alert("Message sent!");
        };

        ws.onmessage = function (evt) {
            alert("About to receive data");
            var received_msg = evt.data;
            alert("Message received = "+received_msg);
        };
        ws.onclose = function () {
            // websocket is closed.
            alert("Connection is closed...");
        };
    };


</script>
</head>
<body style="font-size:xx-large" >
<div>
<a href="#" onclick="connect()">Click here to start connection</a></div>
<a href="webSocketTest:">First Open program</a> 

</body>
</html>

UPDATE: Thanks to adam i have checked the f12 Developer console and I get an error: SCRIPT5022: SecurityError

Community
  • 1
  • 1
SammyG
  • 299
  • 1
  • 4
  • 15
  • `The page loads but the js doesn't. ` - I'm sure it loads, it probably just doesn't run – Jaromanda X Aug 06 '15 at 10:58
  • Which version of IE? – JJJ Aug 06 '15 at 10:59
  • Tested on IE11 and also on Edge. Edge never gives a popup but im assuming its the same problem. – SammyG Aug 06 '15 at 11:00
  • Could you try change your doctype to . As the WebSocket is part of the HTML5 initative, i could imagine that maybe IE blocks it because you file is not a valid HTML5 document. Sadly i cant test it myself at the moment. – Boot750 Aug 06 '15 at 11:18
  • I have tried, but not succeeded. The f12 Developer console give a HTML1527: DOCTYPE expected. Consider adding a valid HTML5 doctype: " ". Ill update the post to what I have changed it to. – SammyG Aug 06 '15 at 11:37

2 Answers2

2

Thanks to the people that have helped.

The Problem is to do with IE and Edge browser not wanting to connect to "Localhost" however if you change Localhost to 127.0.0.1 it works perfectly.

Updated Code:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
    <script type="text/javascript">
        function connect() {
            var ws = new WebSocket("ws://127.0.0.1:8080/service");
            ws.onopen = function () {
                alert("About to send data");
                ws.send("Hello World"); // I WANT TO SEND THIS MESSAGE TO THE SERVER!!!!!!!!
                alert("Message sent!");
            };

            ws.onmessage = function (evt) {
                alert("About to receive data");
                var received_msg = evt.data;
                alert("Message received = "+received_msg);
            };
            ws.onclose = function () {
                // websocket is closed.
                alert("Connection is closed...");
            };
        };


    </script>
</head>
<body style="font-size:xx-large" >
    <div>
    <a href="#" onclick="connect()">Click here to start connection</a></div>
<a href="webSocketTest:">First Open program</a> 

</body>
</html>
SammyG
  • 299
  • 1
  • 4
  • 15
0

Change

ws://localhost:8080/service

to

ws://{your_remote_domain_host}:8080/service

and it should work

adam.pilacki
  • 101
  • 6
  • why would it be on the domin? I am wanting it to connect over localhost to a sever running on the local machine not the webserver – SammyG Aug 06 '15 at 11:09
  • You wrote: 'put it on my remote webserver' - so remote or local? :D – adam.pilacki Aug 06 '15 at 11:13
  • The html file is hosted on a webserver but the connection it is making is still local to the computer viewing the website. It works perfectly in Chrome :/ I will edit the question to make it more clear. – SammyG Aug 06 '15 at 11:19
  • I see - so websocket server is on your local machine... so is there some error message in console - I mean IE js console as well as server console? – adam.pilacki Aug 06 '15 at 11:25
  • Never thought to look at the developer console in IE. yea it has an error SCRIPT5022: SecurityError. Ill update the post thanks – SammyG Aug 06 '15 at 11:28