Reputation: 63
I put together a Java socket server and client that can send messages to each other, I want to use JavaScript as a client, but... Here is what happens when I'm hosting the Java server and load the JavaScript client.
JavaScript:
var connection = new WebSocket('ws://127.0.0.1:9005');
connection.onopen = function () {
connection.send('Ping');
};
This Prints in the chrome console:
WebSocket connection to 'ws://127.0.0.1:9005/' failed: Error during WebSocket handshake: Invalid status line
WebSocket Error [object Event]
What I'm trying to do is send "Ping" to the Java server, but instead it sends all this stuff than disconnects...
This prints in the Java server console:
Bread Server running...
New Connection From: 127.0.0.1:51948
127.0.0.1:51948: GET / HTTP/1.1
127.0.0.1:51948: Host: 127.0.0.1:9005
127.0.0.1:51948: Connection: Upgrade
127.0.0.1:51948: Pragma: no-cache
127.0.0.1:51948: Cache-Control: no-cache
127.0.0.1:51948: Upgrade: websocket
127.0.0.1:51948: Origin: http://markstuff.net
127.0.0.1:51948: Sec-WebSocket-Version: 13
127.0.0.1:51948: User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36
127.0.0.1:51948: Accept-Encoding: gzip, deflate, sdch
127.0.0.1:51948: Accept-Language: en-US,en;q=0.8
127.0.0.1:51948: Sec-WebSocket-Key: D4Epyc7LwvPdfeDWG0sY2g==
127.0.0.1:51948: Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
127.0.0.1:51948:
ERROR: Software caused connection abort: recv failed
JavaScript Client:
markstuff.net/socket/ - markstuff.net/socket/client.js
Java Server:
pastebin com cdZjUWQp
Java Client:
pastebin com efEeezcR
Upvotes: 6
Views: 13093
Reputation: 639
Sounds like you are trying to connect a Websocket client to a bare socket listener in server side. notice that Websocket is not equal to socket. Websocket is a protocol on top of a tcp connection (like how http is on top of tcp) .
so you should either change your server implementation to a Websocket (you can find many java Websocket libraries in internet) or change your client to a socket.
I'm not sure how to create a socket connection in JS but this topic maybe helpful for this problem: Connecting to TCP Socket from browser using javascript
Upvotes: 9