Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you connect an HTML5 web socket to a Java Socket?

I had set up a system that had a Java program running on a server and a Java applet embedded in a page on a client's browser and the two communicating via Java sockets. I'm wondering if I can switch over from a Java applet to just HTML5 and javascript, using a WebSocket on the client side for communication with the Java socket on the server.

Is there a simple way to make a WebSocket communicate with a Java Socket?

like image 712
BCarpe Avatar asked Feb 10 '12 14:02

BCarpe


1 Answers

Is there a simple way to make a WebSocket communicate with a Java Socket?

From what I understand, WebSocket works by the client side opening a port 80 connect to the server side, and sending a variant HTTP 1.1 request to the server to negotiate a WebSocket connection. If the server recognizes this, it will send a suitable response, and then allow the still open TCP connection to be used for full-duplex client-server interactions.

It looks like it would be possible to quickly put together a server-side that just understood WebSocket negotation and not full HTTP. However, I think you are better off looking at existing WebSocket implementations, including those embedded in HTTP servers / protocol stacks.

This Wikipedia page compares a number of WebSocket implementations, and should help you in deciding which server-side implementation to use.

But to directly answer your literal question, a WebSocket client can only connect to a WebSocket-aware server; i.e. that one that can perform the initial negotiation. (On the client side, you could implement starting from a bare Socket, but you would need to implement all of the "HTTP stuff" on top of that ... for the setup phase.)

like image 91
Stephen C Avatar answered Oct 13 '22 01:10

Stephen C