Kyl*_*ler 4 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.
Run Code Online (Sandbox Code Playgroud)
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.
}
}
}
Run Code Online (Sandbox Code Playgroud)
van*_*nje 10
不要对 WebSocket 的名称感到困惑。TCP 套接字和 WebSocket 是完全不同类型的“套接字”。
在 Java 中,您将ServerSocket用于 TCP 套接字。TCP 是一种传输层协议,用于实现 POP3 和 HTTP 等应用层协议。
WebSocket 是 Web 服务器和 Web 浏览器中常用的 HTTP/1.1 协议升级。您不能将 ServerSocket 用于 WebSocket 协议,至少不像您想象的那么直接。首先,您必须实现 HTTP/1.1 协议,然后在其上实现 WebSocket 协议。
在 Java 世界中,您可以使用 Tomcat 或 Jetty 等 Web 服务器,它们提供 WebSocket 实现和高级 Java API。此 API 是 Java 企业版 (JEE) 的一部分。另请参阅Java EE 7 教程 - 第 18 章 Java API for WebSocket。
例如,Jetty 是一个轻量级的 JEE Web 服务器,它可以嵌入到您的应用程序中或作为独立服务器运行。请参阅Jetty 开发指南 - 第 26 章 WebSocket 介绍。
因此,在运行在启用了 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);
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用@ServerEndpoint注释将您的类注册为特定路径的 WebSocket 处理程序。您的 WebSocket URL 是HTTPS 连接ws://host:port/context/toUpper或wss://host:port/context/toUpper用于 HTTPS 连接。
编辑: 这是一个非常简单的 HTML 页面,用于演示与上述 WebSocket 的客户端连接。此页面由与 WebSocket 相同的网络服务器提供服务。包含 WebSocket 的 Web 应用程序部署在本地主机端口 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>
Run Code Online (Sandbox Code Playgroud)
如果您愿意使用Java Spring - 我认为这非常适合您的用例,那么设置 Websocket 服务器和客户端连接非常容易。
这里有一个例子 - https://spring.io/guides/gs/messaging-stomp-websocket/
| 归档时间: |
|
| 查看次数: |
15608 次 |
| 最近记录: |