本文介绍了您可以将 HTML5 Web 套接字连接到 Java 套接字吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了一个系统,该系统有一个运行在服务器上的 Java 程序和一个嵌入在客户端浏览器页面中的 Java 小程序,并且两者通过 Java 套接字进行通信.我想知道是否可以从 Java 小程序切换到 HTML5 和 javascript,在客户端使用 WebSocket 与服务器上的 Java 套接字进行通信.

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.

有没有一种简单的方法可以让 WebSocket 与 Java Socket 通信?

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

推荐答案

据我所知,WebSocket 的工作原理是客户端打开一个连接到服务器端的 80 端口,并向服务器发送一个变体 HTTP 1.1 请求以协商 WebSocket 连接.如果服务器识别出这一点,它将发送一个合适的响应,然后允许将仍然打开的 TCP 连接用于全双工客户端 - 服务器交互.

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.

看起来可以快速组合一个只是理解 WebSocket 协商而不是完整 HTTP 的服务器端.但是,我认为您最好查看现有的 WebSocket 实现,包括嵌入在 HTTP 服务器/协议栈中的那些.

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.

这个维基百科页面比较了许多 WebSocket 实现,应该可以帮助您决定哪个要使用的服务器端实现.

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

但是要直接回答您的字面问题,WebSocket 客户端只能连接到 WebSocket 感知服务器;即可以执行初始协商的那个.(在客户端,您可以从一个裸 Socket 开始实现,但您需要在此之上实现所有HTTP 内容"……在设置阶段.)

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

这篇关于您可以将 HTML5 Web 套接字连接到 Java 套接字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 00:07