Java websocket host?
Asked Answered
A

5

12

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!");
}
Amen answered 27/11, 2010 at 16:30 Comment(2)
Is there any reason you don't use an existing Websockets server?Chutzpah
See this question for list of ready-made websockets servers: #4278956Duotone
R
11

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.

Riehl answered 27/11, 2010 at 16:41 Comment(0)
S
3

Try this lib - 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.

On web side use Socket.IO client javascript lib:

<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>
Sorrow answered 7/5, 2012 at 14:11 Comment(0)
Z
2

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.

I am one of the co-creators of Bristleback Server.

Zenithal answered 2/10, 2012 at 7:40 Comment(0)
S
1

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.

Sym answered 5/9, 2017 at 20:29 Comment(1)
Ivo Wetzel answered the question to this effect, seven years ago.Amen
J
-2

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.

Jointer answered 27/11, 2010 at 16:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.