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

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-27 02:25:56

问题


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?


回答1:


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.)




回答2:


Nope, you cannot communicate using regular sockets with client WebSockets.

WebSockets are special HTTP requests, with an upgrade in the HTTP Header, and a standard protocol to establish a connection (see the official RFC doc).



来源:https://stackoverflow.com/questions/9229426/can-you-connect-an-html5-web-socket-to-a-java-socket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!