Java websocket主机? [英] Java websocket host?

查看:95
本文介绍了Java websocket主机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试一些多人游戏的想法,我正在尝试创建一个Java应用程序来提供基于Web浏览器的多人游戏。

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.

我的开发环境是主机上的Eclipse,以及笔记本电脑上的记事本+谷歌浏览器。

My development environment is Eclipse on the main machine, and notepad + Google Chrome on this laptop.

我在客户端使用javascript创建websocket,并使用java.net 。服务器端的套接字。

I'm creating the websocket using javascript at the client end, and using the java.net.Socket at the server end.

我设法在两端确认连接,但似乎无法发送或接收它们之间的任何数据客户端关闭连接(甚至没有错误;只是看起来吓坏了并调用socket.close)。

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

有没有人有任何想法?

以下是一些代码:

客户:

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

服务器:

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!");
}


推荐答案

而不是痛苦的方式在Java中实现规范,我建议您使用一个现有的解决方案,如 jWebSocket

Rather than going the painful way of implementing the spec in Java, I'd suggest that you use an existing solution like jWebSocket.

如果你不喜欢不介意离开Java土地,我还建议您查看服务器的 Node.js

Also if you don't mind leaving Java land, I'd also suggest that you take a look at Node.js for your Server.

在JavaScript中同时执行服务器和客户端将为您节省大量时间和大量代码,特别是因为JSON不适合静态土地。在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.

有关WebSocket的更多信息可以在常见问题解答。如果您想开始使用Node.js,请查看 TagWiki

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.

无耻插件

对于使用Node.js编写的两个多人游戏,请看看我的 GitHub页面

For two multiplayer games that were written using Node.js take a look at my GitHub page.

这篇关于Java websocket主机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆