Socket Server - Sending messages from client to client - actionscript-3

I have a socket server set up using Adobe AIR, and I am trying to allow two clients to send messages between each other using the server (this is an Android based project so the server has to act as the middle man on a PC). For some reason, the messages I am sending are only being sent back to the same client it came from, rather than to the other one as well. I have already set up a system to identify where each message has come from, and how to deal with it on the other side.
The variable 'connectionNum' int basically represent whether the client is number 0 or 1, and the data being sent to and from the server has either 0, or 1 in front of it.
Ideally I would like a way to direct data to one specific client at a time, rather than attempting to send it to both with the int at the start of every message.
At the moment, only the second client to connect's messages are actually sent through the server, the first sends a blank message, not sure why.

At the moment, only the second client to connect's messages are actually sent through the server, the first sends a blank message, not sure why.
It is because you only have one clientSocket object on the server and you are overwriting it when someone else connects, so when the second client connects you are losing the first clients socket. When you call sendData on the server it is always using clientSocket which is whoever connected last.
To fix this you need an array of clientSocket objects on the server. Then you can pick the correct one to send a message to or send the message to all clients if you want to broadcast a message.

Related

Supporting Server Sent Events with Netty HTTP2 implementation

I am using Netty 4.1-Beta6 version.
I want to support the use case where the HTTP2 server should be able to push events to the HTTP2 client on an existing connection - this could be an alarm or timer event from the cloud which needs to be propagated to the client.
I am thinking of using 'Server Sent Event' feature - is it possible to do this with HTTP2 in Netty, if so, how? Should I keep a http2 stream open by sending data frames with 'final frame' flag set to false? When I try this, what I observe is that the content gets buffered. The data frame doesn't reach the client as and when I write. I am using the DefaultHttp2Encoder. I tried setting the 'Transfer-Encoding' header to 'chunked' too.
Related question - does HTTP2 allows bi-directional data frames once the stream is in 'open' state? The idea is that the server should be able ask data from the client and client should be able to respond with the data in the same stream (reversal of client/server role once the stream is established). Is this possible?
Thanks in advance for the help.
I played with Netty bit more. Here is what I found for the 2 questions above.
Both the client and server can keep the stream in 'open' state by sending 'endOfStream' as false when they send the header/data frames. In order to avoid the buffering of data on the server side, I had to invoke flowController.writePendingBytes() followed by ChannelHandlerContext.flush()'.
I have uploaded my sample here - https://github.com/skssfo/http2
Yes, the client and the server can keep the stream open and send data frames independent of each other.
I am playing with Netty for the first time, it is very cool. Nice job Netty team!

Compress json string for gcm push notification message

I have problem with json data and gcm push notification.
I send big json data and it exceed 4kb limit, so is there any compression that i can use to compress that json(gzip or something like that) and send it?
If data is that big then you should use GCM only as informer to the application that it needs to pull some updated data from server.
So use a tickle message to notify the device that there is something new at server end.
When you receive that tickle message from server, make a pull request to the server in response of which do the needful (Generate a notification/ Update Db or whatever needed)

Mandrill webhooks timeout error

I have been using Mandrill webhooks from a long time and till now I haven't encountered this error.
But now I see this error, I am not sure what has caused this ?
Please let me know why this might be happening and what might be the possible solution for the same.
Is it related to my server handling capacity because I have checked for that as well and Mandrill doesnt have too many concurrent request that it is sending to my Apache server, so according to me that is not an issue and also mysql also doesn't seem to be causing the bottleneck, but then I I have not used any benchmarking tool to determine the same.
Please let me know the solution if you guys have encountered something like this.
It seems that the URL is not responding to the request. There could be a few reasons:
If the URL points to an internal server, a firewall could be blocking it or a port number (if given).
Once set up the webhook will send via a POST HTTP verb, however for testing it sends a HEAD request. Quite often web servers (e.g. IIS) will limit what verbs they respond to and will only respond to GET and POST requests.
If that's working your URL should respond with just headers only to acknowledge the request. (HEAD doesn't allow any page content to be sent) so it should only do something like this for a HEAD request:
<?php header( 'Content-Type:' ); // returning 200 ?>
More details on their site
http://help.mandrill.com/entries/22024856-Why-can-t-my-webhook-or-inbound-route-URL-be-verified-
You may wish to try this tool to see what HTTP header result is being returned (if any) or if another error is being returned, just remember that if the URL is internal, it could be blocked to the outside world.
https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm?hl=en

is it possible to send a data when a websocket connection is opened

I am implementing a Jetty Websocket servlet.
When the server receives a new connection, I want to send a message that will be read by websocket's onopen function. I want this message to be sent only during the open and not using the regular connection.SendMessage() function. Is it possible to do that? and how?
Don't forget the query string. It's valid in WebSocket url.
new Websocket('ws://yoursite.com/path?a=1&b=2&c=3')
Then you can easily parse this url on server side to retrieve the data.
There is no support for this in the protocol but you could fudge something yourself.
When your server completes a handshake, store the initial message you want to deliver to a client.
In your client's onopen function, send a "read initial message" request.
In your server, check that this client hasn't read its initial message; respond with the message; set a flag saying that the initial message has been sent.
Your client and server are both now free to send other messages.

HTTP GET from socket multiple events?

How do you know if a socket server or web server is done transmitting a HTTP GET request when using ProgressEvent.SOCKET_DATA ?
I doing my socket request with socket.writeUTFBytes('GET /index.php HTTP/1.1\r\n');
But the 'answer' is so big that i get multiple ProgressEvent.SOCKET_DATA. How do i know how much data it is supposed to transmit to me ? Or when it's done transmitting ?? Or even how many progressEvents i will get out of this request ? So far I'm using a timer that checks if the server is still transmitting but this isn't a very clean way of doing things..
How do i know how much data it is supposed to transmit to me? Or when it's done transmitting ??
By reading the Content-length header if that is sent by the server, or by waiting until the server closes the connection, or by reading until you've encountered a last-chunk (0<CRLF><CRLF>) if chunked transfer encoding is enabled, or any of the other indications that a full response has been received.
For simplicity, use a HTTPService or if that doesn't fit your needs, use a library that implements an HTTP client.
Or even how many progressEvents i will get out of this request ?
There is no way to tell.