Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Communication with JS websockets #26

Closed
matkoch opened this issue May 11, 2020 · 7 comments
Closed

Communication with JS websockets #26

matkoch opened this issue May 11, 2020 · 7 comments

Comments

@matkoch
Copy link

matkoch commented May 11, 2020

Thanks for your work with this library. Really appreciate it.

I'm trying to communicate with a JS WebSocket. Roughly like:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <!-- <script src="app.js" type="text/javascript"></script> -->
    <script type="text/javascript">
        var socket = new WebSocket('http://localhost:8083/socket');
        socket.onopen = function() {
           // alert('handshake successfully established. May send data now...');
		   socket.send("Hi there from browser.");
        };
		socket.onmessage = function (evt) {
                //alert("About to receive data");
                var received_msg = evt.data;
                alert("Message received = "+received_msg);
            };
        socket.onclose = function() {
            alert('connection closed');
        };
    </script>
</body>
</html>

But http:// is not supported: SyntaxError: An invalid or illegal string was specified

This SO question seems to be related, but I still couldn't make it work.

Do you have any hints?

@jchristn
Copy link
Owner

Hi @matkoch please try changing var socket = new WebSocket('http://localhost:8083/socket'); to var socket = new WebSocket('ws://localhost:8083/socket');. I took a screenshot below showing it working on mine using the TestServer project.

image

Please let me know if this works!

@matkoch
Copy link
Author

matkoch commented May 11, 2020

Interestingly, it does only work in Chrome, but not in Firefox.

@jchristn
Copy link
Owner

Is this what you get in Firefox?

var socket = new WebSocket('ws://localhost:8000/socket');
[Exception... "<no message>"  nsresult: "0x805e0006 (<unknown>)"  location: "JS frame :: debugger eval code :: <TOP_LEVEL> :: line 1"  data: no] debugger eval code:1:14
    <anonymous> debugger eval code:1
Content Security Policy: The page’s settings blocked the loading of a resource at ws://localhost:8000/socket (“connect-src”). debugger eval code:1:13​

It may be related to this:
image

Discovered these: https://stackoverflow.com/questions/37298608/content-security-policy-the-pages-settings-blocked-the-loading-of-a-resource
and
eclipse-che/che#13736

@jchristn
Copy link
Owner

jchristn commented May 11, 2020

Oh! Try going to another page. When I first went to my own site (www.joelchristner.com) i.e. any site that doesn't have this content security policy meta tag, it worked!

image

@jchristn
Copy link
Owner

Also interesting to note that Chrome starts outbound websocket connections using IPv6 whereas Firefox is using IPv4.

@jchristn
Copy link
Owner

Going to close this, hope it helps. Seems the library is behaving as it should. Please re-open if you have further issues @matkoch !

@matkoch
Copy link
Author

matkoch commented May 12, 2020

Yep. Thanks for your response! I use this only in combination with CEF, so I’m good :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants