0

I have a question about GWT. Lets say that my GWT client app needs to connect to a TCP socket, do some stuff and close it. Now its barely possible within GWT as sockets does not work on client side.

But actually I can create socket in server side of application, like this:

package com.kurjerzy.testGWT.server;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.Socket;

import com.kurjerzy.testGWT.client.GreetingService;
import com.kurjerzy.testGWT.shared.FieldVerifier;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
        GreetingService {

    public String greetServer(String input) throws IllegalArgumentException {
        // Verify that the input is valid. 
        if (!FieldVerifier.isValidName(input)) {
            // If the input is not valid, throw an IllegalArgumentException back to
            // the client.
            throw new IllegalArgumentException(
                    "Name must be at least 4 characters long");
        }

        String serverInfo = getServletContext().getServerInfo();
        String userAgent = getThreadLocalRequest().getHeader("User-Agent");

        // Escape data from the client to avoid cross-site script vulnerabilities.
        input = escapeHtml(input);
        userAgent = escapeHtml(userAgent);

        String rStr = null;

        try {
            InetAddress addr = Inet4Address.getByName( "google.com" );
            Socket s = new Socket( addr , 80 );
            rStr = "connected!";
        } catch ( Exception e ) {
            rStr = "e: " + e.getMessage();
        }

        return "Hasdasdello, " + input + "!<br><br>I am running " + serverInfo
                + ".<br><br>It looks like you are using:<br>" + userAgent + "<br>rStr=" + rStr;
    }

    /**
     * Escape an html string. Escaping data received from the client helps to
     * prevent cross-site script vulnerabilities.
     * 
     * @param html the html string to escape
     * @return the escaped string
     */
    private String escapeHtml(String html) {
        if (html == null) {
            return null;
        }
        return html.replaceAll("&", "&amp;").replaceAll("<", "&lt;")
                .replaceAll(">", "&gt;");
    }
}

But unfortunately serverlet does not have a permission to create a socket connection and it raises exception.

Any ideas how to implement this?

The thing I want to achieve is:

1) having native-java, android and gwt real time game clients that make connection to server

2) having a server that serves logic for all clients on one time

so native-java and android clients may connect to server using tradional tcp socket. now I would want to allow connecting to same game server from html5 environment, but I cannot figure out what connection transport should I use...

the possible mine ideas are:

a) make serverlet accept rpc call and communicate with gameserver using socket (but how to pass that permission denited?..)

b) make gwt client use some 3rd party javascript socket library and connect directly to gameserver (but which one is good to use?)

maybe you have better ideas?

Game is turn based so its not time-critical code, but data is transferred realtime (lag is acceptable but connection must be maintained all the time to inform 2nd player about first player moves)

Thanks in advance.

1 Answer 1

0

Did you investigate Web Sockets API in html5 -

1) http://code.google.com/p/gwt-comet/wiki/WebSockets ( Chrome Only )

2) What browsers support HTML5 WebSocket API?

3) http://www.html5rocks.com/en/tutorials/websockets/basics/

Also, you might give PlayN , Titanium , PhoneGAP etc a quick look if you have not already. Your question indicates you are trying to handle low level stuff by hand. Browse through the above technologies. They might have solved these problems already.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.