Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!");
}
like image 931
Toby Wilson Avatar asked Nov 27 '10 16:11

Toby Wilson


People also ask

Is Java Socket A WebSocket?

A TCP socket and a WebSocket are entirely different kind of "sockets". In Java you use a ServerSocket for TCP sockets. TCP is a transport layer protocol used to implement application layer protocols like POP3 and HTTP. WebSocket is a HTTP/1.1 protocol upgrade commonly used in web servers and web browsers.

Do I need a server for WebSocket?

By definition websockets like normal sockets are client-server so yes, you need a server. However there is an alternative to waiting for Apache plugins. I am using a hosted server http://www.achex.ca. Its free and you have tutorials in javascript on how to use the server.

What is STOMP Java?

STOMP. Stream Text-Oriented Messaging Protocol (STOMP) is a simple, interoperable wire format that allows client and servers to communicate with almost all the message brokers. It is an alternative to AMQP (Advanced Message Queuing Protocol) and JMS (Java Messaging Service).


2 Answers

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.

like image 109
Ivo Wetzel Avatar answered Oct 02 '22 21:10

Ivo Wetzel


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>
like image 35
Nikita Koksharov Avatar answered Oct 02 '22 20:10

Nikita Koksharov