Java 本地主机上的 Apache Tomcat websockets 实现

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22399471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:31:51  来源:igfitidea点击:

Apache Tomcat websockets implementation on localhost

javatomcatservletswebsocket

提问by Padfoot

Hey guys I'm trying to learn websockets. Tried a few examples given on apache tomcat 7 websockets examples -> 'http://tomcat.apache.org/tomcat-7.0-doc/web-socket-howto.html'. I'm done with the client-side code. having a problem with the server-side one which is to be written in a java servlet. I tried to run the examples on my localhost using eclipse and Apache Tomcat 7.0.52. Can anyone help me with some tutorials and examples for the server code.

嘿伙计们,我正在尝试学习 websockets。在 apache tomcat 7 websockets 示例中尝试了一些示例 -> ' http://tomcat.apache.org/tomcat-7.0-doc/web-socket-howto.html'。我已经完成了客​​户端代码。服务器端的问题是用 java servlet 编写的。我尝试使用 Eclipse 和 Apache Tomcat 7.0.52 在我的本地主机上运行这些示例。任何人都可以帮助我提供服务器代码的一些教程和示例。

Here is the client side code for a simple echo server:

这是一个简单的回显服务器的客户端代码:

var wsUri = "ws://echo.websocket.org/"; 
var output; 

function init() { 
output = document.getElementById("output");
$('?#?send?').click(function(){
doSend("ja ghari");
});
testWebSocket(); } 

function testWebSocket() {
websocket = new WebSocket(wsUri); 
websocket.onopen = function(evt) { onOpen(evt);};
websocket.onclose = function(evt) { onClose(evt) ;}; 
websocket.onmessage = function(evt) { onMessage(evt) ;}; 
websocket.onerror = function(evt) { onError(evt) ;}; } 

function onOpen(evt) { 
writeToScreen("CONNECTED"); 
doSend("WebSocket rocks");
} 

function onClose(evt) { 
writeToScreen("DISCONNECTED"); } 

function onMessage(evt) { 
writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data+'</span>'); 
} 

function onError(evt) { 
writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data); } 

function doSend(message) { 
writeToScreen("SENT: " + message); 
websocket.send(message); } 

function writeToScreen(message) { 
var pre = document.createElement("p"); pre.style.wordWrap = "break-word"; 
pre.innerHTML = message; output.appendChild(pre); } 

window.addEventListener("load", init, false); 

now i need to write a server side code in java for this I need a server which will communicate with my client using websockets

现在我需要在 Java 中编写一个服务器端代码,我需要一个使用 websockets 与我的客户端进行通信的服务器

Apache Tomcat 7 or above is a must, server side code to be written in java. Thanks in advance.

Apache Tomcat 7 或更高版本是必须的,服务器端代码要用 java 编写。提前致谢。

回答by Mark Thomas

Apache Tomcat 7 ships with an example WebSocket echo implementation.

Apache Tomcat 7 附带了一个示例 WebSocket 回声实现。

You don't write the server side as a Servlet. You need to implement a WebSocket endpoint. Tomcat will handle the plumbing to route the request to the endpoint.

您不会将服务器端编写为 Servlet。您需要实现一个 WebSocket 端点。Tomcat 将处理管道以将请求路由到端点。