Friday, May 27, 2011

Re: GWT client server communication

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
>
> On Fri, May 27, 2011 at 6:38 AM, J.Ganesan <j.gane...@datastoregwt.com>wrote:
>
>
>
>
>
>
>
>
>
> > You can replicate the  TCP client server communication in your
> > existing code in GWT with websockets.
>
> > J.Ganesan
> >www.DataStoreGwt.com
>
> > On May 27, 10:36 am, ham <hamish.tus...@gmail.com> wrote:
> > > 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.
>
> > --
> > 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-web-toolkit@googlegroups.com.
> > To unsubscribe from this group, send email to
> > google-web-toolkit+unsubscribe@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.

--
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-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment