java - 如何在 Java 中实现 WebSocket 服务器?

标签 java websocket server

我正在为通信应用程序设置我的第一个 websocket 服务器。我似乎无法弄清楚 websockets 是如何在 Java 中实现的。

我尝试过创建基于注释的端点,但未成功,但我不确定客户端信息将来自何处。这基本上是我的代码的要点,没有涉及世俗的细节。

我正在尝试让 MessageHelper 类处理 websocket 信息传输,我只是无法掌握如何实际在那里进行传输。

class MainServer implements Runnable {
// VARIABLES
    ServerSocket serverSocket = null;
    int port;
// CONSTRUCTORS
    MainServer(int p) {
        this.port = p;
    }
// METHODS
    public void run() {
        openServerSocket();
        while(!isStopped()){
            try{
                clientSocket = serverSocket.accept();
            } catch(IOException e) {
                // Do something
            }
            new Thread(new MainThread(clientSocket)).start();
        }
    }
}

// Other methods below.
public class MainThread {

    final Socket socket;


    MainThread(Socket s) {
        this.socket = s;
    }

    public void run() {
        try{
            BufferedReader br = new BufferedReader(
                new InputStreamReader(socket.getInputStream()));

            String input = br.readLine(), read = br.readLine();
            while(!input.isEmpty()) {
                read += "\n";
                read += input;
                input = br.readLine();
            }

            /**
            *  Everything works fine, I'm just not sure where to go
            * from here. I tried creating MessageHelper into the java
            * websocket implementation using annotations but it did not 
            * accept input from the client after the handshake was
            * made. My client would send something but it would just 
            * give and EOFException.
            **/
            if(websocketHandshakeRequest(read)) {
                MessageHelper messageHelper = 
                    new MessageHelper(this.socket);
            } else {
                // Do something
            }
        } catch(Exception e) {
            // Do something.
        }
    }
}

最佳答案

不要对 WebSocket 的名称感到困惑。 TCP 套接字和 WebSocket 是完全不同的“套接字”。

在 Java 中,您使用 ServerSocket用于 TCP 套接字。 TCP 是一种传输层协议(protocol),用于实现 POP3 和 HTTP 等应用层协议(protocol)。

WebSocket 是一种 HTTP/1.1 协议(protocol)升级,常用于 Web 服务器和 Web 浏览器。您不能将 ServerSocket 用于 WebSocket 协议(protocol),至少不像您想象的那么简单。首先,您必须实现 HTTP/1.1 协议(protocol),然后在此之上实现 WebSocket 协议(protocol)。

在 Java 世界中,您可以使用提供 WebSocket 实现和 high level Java API 的 Web 服务器,例如 Tomcat 或 Jetty。 .此 API 是 Jave 企业版 (JEE) 的一部分。另见Jave EE 7 Tutorial - Chapter 18 Java API for WebSocket .

例如。 Jetty 是一个轻量级的 JEE Web 服务器,可以嵌入到您的应用程序中或作为独立服务器运行。见 Jetty Development Guide - Chapter 26. WebSocket Introduction .

因此,在启用了 WebSocket 的 JEE Web 服务器(如 Jetty)中运行的 Java Web 应用程序中,您可以按如下方式实现服务器端 WebSocket:

package com.example.websocket;

import org.apache.log4j.Logger;

import javax.websocket.CloseReason;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;

@ServerEndpoint("/toUpper")
public class ToUpperWebsocket {

  private static final Logger LOGGER = Logger.getLogger(ToUpperWebsocket.class);

  @OnOpen
  public void onOpen(Session session) {
    LOGGER.debug(String.format("WebSocket opened: %s", session.getId()));
  }

  @OnMessage
  public void onMessage(String txt, Session session) throws IOException {
    LOGGER.debug(String.format("Message received: %s", txt));
    session.getBasicRemote().sendText(txt.toUpperCase());
  }

  @OnClose
  public void onClose(CloseReason reason, Session session) {
    LOGGER.debug(String.format("Closing a WebSocket (%s) due to %s", session.getId(), reason.getReasonPhrase()));
  }

  @OnError
  public void onError(Session session, Throwable t) {
    LOGGER.error(String.format("Error in WebSocket session %s%n", session == null ? "null" : session.getId()), t);
  }
}

您使用 @ServerEndpoint 将您的类注册为特定路径的 WebSocket 处理程序。注解。你的 WebSocket URL 是 ws://host:port/context/toUpperwss://host:port/context/toUpper用于 HTTPS 连接。

编辑:
这是一个非常简单的 HTML 页面,用于演示与上述 WebSocket 的客户端连接。此页面由与 WebSocket 相同的网络服务器提供服务。包含 WebSocket 的 Web 应用程序部署在 localhost 端口 7777 上的上下文“websocket”中。
    <html>
    <body>
    <h2>WebSocket Test</h2>
    <div>
    <input type="text" id="input" />
    </div>
    <div>
    <input type="button" id="connectBtn" value="CONNECT" onclick="connect()" />
    <input type="button" id="sendBtn" value="SEND" onclick="send()" disable="true" />
    </div>
    <div id="output">
    <h2>Output</h2>
    </div>
    </body>
    <script type="text/javascript">
    var webSocket;
    var output = document.getElementById("output");
    var connectBtn = document.getElementById("connectBtn");
    var sendBtn = document.getElementById("sendBtn");
    var wsUrl = (location.protocol == "https:" ? "wss://" : "ws://") + location.hostname + (location.port ? ':'+location.port: '') + "/websocket/toUpper";

    function connect() {
      // open the connection if one does not exist
      if (webSocket !== undefined
        && webSocket.readyState !== WebSocket.CLOSED) {
        return;
      }

      updateOutput("Trying to establish a WebSocket connection to <code>" + wsUrl + "</code>");

      // Create a websocket
      webSocket = new WebSocket(wsUrl);

      webSocket.onopen = function(event) {
        updateOutput("Connected!");
        connectBtn.disabled = true;
        sendBtn.disabled = false;
      };

      webSocket.onmessage = function(event) {
        updateOutput(event.data);
      };

      webSocket.onclose = function(event) {
        updateOutput("Connection Closed");
        connectBtn.disabled = false;
        sendBtn.disabled = true;
      };
    }

    function send() {
      var text = document.getElementById("input").value;
      webSocket.send(text);
    }

    function closeSocket() {
      webSocket.close();
    }

    function updateOutput(text) {
      output.innerHTML += "<br/>" + text;
    }
    </script>
    </html>

Sample WebSocket webpage rendered in Firefox

关于java - 如何在 Java 中实现 WebSocket 服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57922238/

相关文章:

websocket - websocket url 中的 EIO=3&transport=websocket 是什么

php - 如何打开wss : or ws: port on my website?

linux - 在网络上设置多个网络服务器

java - 在android上实现离散小波变换

java - 将 Mockito 与 Kotlin 一起使用时,如何绕过 any() 不得为空?

javascript - EventSource(服务器发送的事件)中的 HTTP 授权 header

javascript - Socket.io事件监听器/事件处理程序Socket.io

javascript - 使用额外的 url 查询参数来防止缓存或强制更新 css/js 是否有效?

java - BeanDefinitionParsingException : Unable to locate Spring NamespaceHandler for XML schema namespace [http://java. sun.com/xml/ns/javaee]

java - 如何从 Java applet 线程调用 JavaScript 回调?