HTML5 WebSocket

GetTriggerUnit-

DogEntrepreneur
Reaction score
129
Ok, so I have a problem. I see the connection message on the server side but not on client side.
I (obviously) use Chrome.

I see the disconnect message (on client side) when I close the server.
Client side:
Code:
<html>
	<head>
		<script type="text/javascript" src="../jQuery.js"></script>
		<script type="text/javascript">
			var ws;
			
			$(document).ready(function () {
				ws = new WebSocket('ws://localhost:12615');
				ws.onopen = function () {
					$('#info').html('<b>Connected</b>');
				};
				ws.onmessage = function (msg) {
					$('#info').html('<b>Message received: </br>' + msg);
				};
				ws.onclose = function () {
					$('#info').html('<b>Disconnected</b>');
				};
			});
		</script>
		<div id="info">
		</div>
	</head>
</html>

Server side:
Code:
package webserver;

import java.net.*;

public class Main {

    public static void main(String[] args) throws Exception {
        ServerSocket ss = new ServerSocket(12615);
        while (true) {
            Socket s = ss.accept();
            System.out.printf("Connected...\n");
            s.getOutputStream().write("Hello!".getBytes());
            s.getOutputStream().flush();
            System.out.printf("Sent...\n");
        }
    }

}
 

phyrex1an

Staff Member and irregular helper
Reaction score
447
Websockets aren't "raw" sockets and needs special attention both on the client side and on the server side. On the client side the Websocket object probably does this for you but on the server you have to take care of it yourself.
A websocket starts with sending a normal http request (some headers and a body) to the server and then expects the server to respond with a set of headers and a body. After that is completed there will be a stream of frames, on the client I expect that the Websocket object takes care of the frame unpacking/packing but on the server side you have to do this yourself. Similarly, closing the socket also needs special code if you want it to happen cleanly.

From here: http://www.whatwg.org/specs/web-socket-protocol/
Code:
   The handshake from the client looks as follows:
        GET /demo HTTP/1.1
        Host: example.com
        Connection: Upgrade
        Sec-WebSocket-Key2: 12998 5 Y3 1  .P00
        Sec-WebSocket-Protocol: sample
        Upgrade: WebSocket
        Sec-WebSocket-Key1: 4 @1  46546xW%0l 1 5
        Origin: http://example.com

        ^n:ds[4U

   The handshake from the server looks as follows:

        HTTP/1.1 101 WebSocket Protocol Handshake
        Upgrade: WebSocket
        Connection: Upgrade
        Sec-WebSocket-Origin: http://example.com
        Sec-WebSocket-Location: ws://example.com/demo
        Sec-WebSocket-Protocol: sample

        8jKS'y:G*Co,Wxa-
You should probably get used to that document before you start implementing a websocket server.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top