GWT client server communication

318 views
Skip to first unread message

ham

unread,
May 27, 2011, 1:36:29 AM5/27/11
to Google Web Toolkit
Hi,
I have this code wherein i have a TCP client server communication
happening.The server reads a file which has integers stored in it and
sends response to each client .The server spawns a new thread for each
client.
I need to make use of GWT to show a fish moving with new updates from
server.
Now i cannot have TCP class on client side as GWT would not allow
socket library on client side.So i create a new TCP client on
server(GWT server) and things work.
But I cannot create multiple TCP clients (multiple tabs opened) as
GWT server is singleton.
So i thought of creating a new TCP client on each new request from
browser but i cannot keep track of which client made the request as
each is a new process and there is no connection between tabs.

So I am little stuck here.

So in short there are 2 problems.
1.Can i have TCP library on client side.(If yes then no need for
second problem)
2.If not then i need to create a new TCP client at GWT server (RPC)
and keep track of which client did that and send updates to that
client.Since each client would have a different data on TCP server
read.

ham

unread,
May 27, 2011, 1:21:32 AM5/27/11
to Google Web Toolkit

Patrick Julien

unread,
May 27, 2011, 9:31:27 AM5/27/11
to google-we...@googlegroups.com
The gwt servlet may be a singleton but it supports a multi-threading environment

>
> So in short there are 2 problems.
> 1.Can i have TCP library on client side.(If yes then no need for
> second problem)

no

J.Ganesan

unread,
May 27, 2011, 9:38:56 AM5/27/11
to Google Web Toolkit
You can replicate the TCP client server communication in your
existing code in GWT with websockets.

J.Ganesan
www.DataStoreGwt.com

hamish tushar chandola

unread,
May 27, 2011, 12:36:19 PM5/27/11
to google-we...@googlegroups.com
Hi Patrick,
Is there a way in GWT to push data from server to client.
Right now i am using RPC asyncallback but for that client needs to make request to server and then it responds.
So its more like pull mechanism .I wud need push .

Thanks 
Hamish

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.




--
~Hamish
~~~~~Hope is a good thing, maybe the best of things, and no good thing ever dies.

hamish tushar chandola

unread,
May 27, 2011, 12:37:00 PM5/27/11
to google-we...@googlegroups.com
Hi Ganesan,

Is there a way in GWT to push data from server to client.
Right now i am using RPC asyncallback but for that client needs to make request to server and then it responds.
So its more like pull mechanism .I wud need push .

Thanks 
Hamish

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

Patrick Julien

unread,
May 27, 2011, 12:40:18 PM5/27/11
to google-we...@googlegroups.com
you can use websockets but not many browsers support it. You can use comet

google "gwt comet"

hamish tushar chandola

unread,
May 27, 2011, 1:57:59 PM5/27/11
to google-we...@googlegroups.com
Hi Patrick,

thanks for the help.
I am now trying to use websocket and there is only one example i cn find online.

I do not understand one thing.
websocket connects to some port.But where is the code to receive the connection.
I do not think this is complete code.
Do you not think we would always get onClose() call back.

Is there  a  complete example where I can look at client server communication using socket in GWT.

thanks
Hamish

David Chandler

unread,
May 27, 2011, 2:13:07 PM5/27/11
to google-we...@googlegroups.com
Have a look at 
David Chandler
Developer Programs Engineer, Google Web Toolkit
w: http://code.google.com/
b: http://googlewebtoolkit.blogspot.com/
t: @googledevtools

J.Ganesan

unread,
May 27, 2011, 8:41:40 PM5/27/11
to Google Web Toolkit
Hi Hamish,
Using Pygmy web server, I could achieve two way communication between
client and server. There are two differences compared to conventional
TCP/IP communication.
1. You have to encode the binary data into string
2. The server push is asynchronous

code segment to establish websocket is given below. The code is in a
Handler in server side.
protected boolean handleBody(HttpRequest request, HttpResponse
response) throws IOException
{

response.addHeader("Upgrade", "WebSocket");
response.addHeader("Connection", "Upgrade");
response.addHeader("Sec-WebSocket-Origin",
request.getRequestHeader("Origin"));

response.addHeader("Sec-WebSocket-Location", "ws://" +
request.getRequestHeader("Host") + '/' );
response.addHeader("Sec-WebSocket-Protocol", "sample");

response.printOutHeaders();

byte[] eightByteA =
request.getInternetInputStream().readNBytes(8);


String key1S = request.getHeaders().get("Sec-WebSocket-Key1");
String key2S = request.getHeaders().get("Sec-WebSocket-Key2");

int key1i = getIntForSecurityKey(key1S);
System.out.println(" key1i " + key1i);
int key2i = getIntForSecurityKey(key2S);

if (key1i == -1 || key2i == -1 || eightByteA == null)
{
System.out.println("skipping key exchange ");
}
else
{
try
{
additionalDataForUpgrade_ = (makeResponseToken(key1i, key2i,
eightByteA));
response.addResponseData( this ) ;
} catch (NoSuchAlgorithmException ne)
{
System.out.println("NoSuchAlgorithmException " + ne);
}
}
return true;
}

protected int getIntForSecurityKey(String keyS)
{
int result = -1;
if (keyS == null || keyS.length() == 0)
{
//
} else
{
Integer spaces = new Integer(0);
Long number = new Long(0);
for (Character c : keyS.toCharArray())
{
if (c.equals(' '))
{
++spaces;
}
if (Character.isDigit(c))
{
number *= 10;
number += Character.digit(c, 10);
}
}
number /= spaces;
result = number.intValue();
}
return result;
}

protected byte[] makeResponseToken(int key1, int key2, byte[] token)
throws NoSuchAlgorithmException
{
MessageDigest md5digest = MessageDigest.getInstance("MD5");
for (Integer i = 0; i < 2; ++i)
{
byte[] asByte = new byte[4];
int key = (i == 0) ? key1 : key2;
asByte[0] = (byte) (key >> 24);
asByte[1] = (byte) ((key << 8) >> 24);
asByte[2] = (byte) ((key << 16) >> 24);
asByte[3] = (byte) ((key << 24) >> 24);
md5digest.update(asByte);
}
md5digest.update(token);
return md5digest.digest();
}

/// to be tweaked depending on the client agent
public static void sendMessage(String message, OutputStream out)
throws IOException
{
synchronized (out)
{
out.write(0x00);
out.write(message.getBytes());
out.write(0xFF);
out.flush();
}
}



Hope this helps.

J.Ganesan
www.DataStoreGwt.com
persistance engine for GWT.

On May 27, 9:37 pm, hamish tushar chandola <hamish.tus...@gmail.com>
wrote:
> Hi Ganesan,
>
> Is there a way in GWT to push data from server to client.
> Right now i am using RPC asyncallback but for that client needs to make
> request to server and then it responds.
> So its more like pull mechanism .I wud need push .
>
> Thanks
> Hamish
>

soetommy

unread,
Jul 14, 2011, 1:35:52 PM7/14/11
to Google-We...@googlegroups.com
Hi,

Can you tell me how you implemented the client and server codes for gwt for
tcp connection to server?

Thanks a lot!

Soe


Reply all
Reply to author
Forward
0 new messages