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
The JavaScript Socket object lets you connect to any server on the Internet and to exchange data with this server. The Socket object provides basic functionality to connect to a remote computer over a TCP/IP network or the Internet.
Create a socket with the socket() system call. Initialize the socket address structure as per the server and connect the socket to the address of the server using the connect() system call. Receive and send the data using the recv() and send(). Close the connection by calling the close() function.
Sockets are commonly used for client and server interaction. Typical system configuration places the server on one machine, with the clients on other machines.
WebSocket is a protocol, Socket is an endpoint - got it thanks. Also, a simple google search returned "The current API specification allowing web applications to use this protocol(WebSocket) is known as WebSockets" (note the plural).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With