Java websocket主机?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4292624/
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
Java websocket host?
提问by Toby Wilson
I'm trying some multiplayer game ideas out at the moment and am trying to create a Java application to serve a web browser based multiplayer game.
我目前正在尝试一些多人游戏的想法,并且正在尝试创建一个 Java 应用程序来为基于 Web 浏览器的多人游戏提供服务。
My development environment is Eclipse on the main machine, and notepad + Google Chrome on this laptop.
我的开发环境是主机上的Eclipse,笔记本上的记事本+谷歌浏览器。
I'm creating the websocket using javascript at the client end, and using the java.net.Socket at the server end.
我在客户端使用 javascript 创建 websocket,在服务器端使用 java.net.Socket。
I've managed to get a connection acknowledged at both ends, but can't seem to send or recieve any data between them without the client closing the connection (doesn't even error; just seems to freak out at something and call socket.close).
我已经设法在两端都确认了连接,但是如果客户端没有关闭连接,似乎无法在它们之间发送或接收任何数据(甚至没有错误;只是似乎对某些事情感到害怕并调用 socket.关闭)。
Does anyone have any ideas?
有没有人有任何想法?
Here's some code:
这是一些代码:
Client:
客户:
<script type="text/javascript">
var socket;
function init() {
socket = new WebSocket("ws://192.168.0.3:10000");
socket.onopen = function() { alert('OPEN: ' + socket.readyState); }
socket.onmessage = function (msg) { alert('DATA: ' + msg.data); }
socket.onerror = function (msg) { alert('DATA: ' + msg.data); }
socket.onclose = function () { alert('CLOSED: ' + socket.readyState); }
}
function onClick() {
socket.send("YAY!");
}
</script>
Server:
服务器:
public static void main(String args[])
{
System.out.printLn("Websocket server test");
ServerSocket connectSocket = null;
try
{
Socket clientSocket;
connectSocket = new ServerSocket(10000);
System.out.printLn("Waiting for connection...");
clientSocket = connectSocket.accept();
System.out.printLn("Got one!");
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
for(int i=0;i<100;i++) //Shit but easy
{
String data = in.readLine();
System.out.printLn("Got data: " + data);
out.printLn("YAY!");
}
}
catch (IOException e)
{
System.out.printLn("You fail: " + e.getMessage());
}
System.out.printLn("Finished!");
}
采纳答案by Ivo Wetzel
Rather than going the painful way of implementing the specin Java, I'd suggest that you use an existing solution like jWebSocket.
与其采取痛苦的方式在 Java中实现规范,我建议您使用现有的解决方案,例如jWebSocket。
Also if you don't mind leaving Java land, I'd also suggest that you take a look at Node.jsfor your Server.
此外,如果您不介意离开 Java 领域,我还建议您为您的服务器查看Node.js。
Doing both Server and Client in JavaScript will save you lots of time and lots of Code, especially since JSON just doesn't fit that well into static land. Also creating multiplayer servers in Node.js is trivial, since the event based, single threaded model fits the whole thing pretty well.
在 JavaScript 中同时执行服务器和客户端将为您节省大量时间和大量代码,尤其是因为 JSON 不太适合静态领域。在 Node.js 中创建多人服务器也很简单,因为基于事件的单线程模型非常适合整个事情。
More information on WebSocket can be found in the FAQ. In case you want to get started with Node.js take a look at the TagWiki.
更多关于 WebSocket 的信息可以在FAQ 中找到。如果您想开始使用 Node.js,请查看TagWiki。
shameless plug follows
无耻的插件跟随
For two multiplayer games that were written using Node.js take a look at my GitHub page.
对于使用 Node.js 编写的两个多人游戏,请查看我的GitHub 页面。
回答by Nikita Koksharov
Try this lib - https://github.com/mrniko/netty-socketio
试试这个库 - https://github.com/mrniko/netty-socketio
Based on high performance socket lib Netty. It supports latest protocol of Socket.IO server. Several transports including websocket.
基于高性能套接字库 Netty。它支持Socket.IO服务器的最新协议。包括websocket在内的几种传输方式。
On web side use Socket.IOclient javascript lib:
在 web 端使用Socket.IO客户端 javascript 库:
<script type="text/javascript">
var socket = io.connect('http://localhost:81', {
'transports' : [ 'websocket' ],
'reconnection delay' : 2000,
'force new connection' : true
});
socket.on('message', function(data) {
// here is your handler on messages from server
});
// send object to server
var obj = ...
socket.json.send(obj);
</script>
回答by voitec
I would suggest our high level solution: Bristleback Server. It contains both server and client, you can choose from several existing low level WebSocket engines (like Jetty, Netty or Tomcat), developing with Bristleback is extremally fast and easy. However, it is still Beta and we are working hard to release a final 1.0.0 version. If you use Maven, we have provided an archetype with ready to use web application.
我会建议我们的高级解决方案:Bristleback Server。它包含服务器和客户端,您可以从几个现有的低级 WebSocket 引擎(如 Jetty、Netty 或 Tomcat)中进行选择,使用 Bristleback 进行开发非常快速和容易。但是,它仍然是 Beta,我们正在努力发布最终的 1.0.0 版本。如果您使用 Maven,我们提供了一个随时可用的 Web 应用程序的原型。
I am one of the co-creators of Bristleback Server.
我是 Bristleback Server 的共同创建者之一。
回答by Peter
As no one yet really answered your question: the reason it does not work, is because you are not implementing the websocket specification. It takes of lot more work to setup a proper websocket connection than just opening a socket, as the websocket connection setup starts with a HTTP upgrade request. Your client is closing the connection, because it does not receive a positive answer on the upgrade request to start with.
由于还没有人真正回答您的问题:它不起作用的原因是因为您没有实现 websocket 规范。设置正确的 websocket 连接需要做更多的工作,而不仅仅是打开一个套接字,因为 websocket 连接设置从 HTTP 升级请求开始。您的客户端正在关闭连接,因为它没有收到有关开始升级请求的肯定答复。
回答by DeLac
I can't help you with sockets, but can i suggest you to use RMI technology? I'm trying to make a multiplayer rpg in java, and i'm using remote method invocation between server and client (it is possible also call-back the client from the server). It's really easy use it, but it uses TCP instead of UDP. In LAN experience there is no lag, on internet I have not tried yet. However, if your game tolerates just a bit retard between request and response, there is no problem. This is the link of my project, Client and Server classes may be useful for you.
我无法帮助您处理套接字,但我可以建议您使用 RMI 技术吗?我正在尝试在 Java 中制作多人 rpg,并且我在服务器和客户端之间使用远程方法调用(也可以从服务器回调客户端)。它真的很容易使用,但它使用 TCP 而不是 UDP。在局域网体验中没有延迟,在互联网上我还没有尝试过。但是,如果您的游戏在请求和响应之间容忍一点延迟,则没有问题。这是我的项目的链接,Client 和 Server 类可能对您有用。