Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
504 views
in Technique[技术] by (71.8m points)

sockets - Java websocket host?

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.

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

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

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

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!");
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

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.

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.

shameless plug follows

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...