question

pingu2k4 avatar image
pingu2k4 asked

Recieving web requests from playfab to aws server

I have a C# dotnetcore console app running on an AWS instance, and I would like to add in communication between this and my playfab cloudscript.

I can communicate from the C# console to playfab, that was simple just using the playfab nuget package. However I'm having trouble going the other way.

I only ever want to send a few different simple messages, so im not looking for anything too complex. What I have done so far, is I added the following to the start of my console application:

var listener = new HttpListener();
listener.Prefixes.Add("http://+:80/");

listener.Start();
Writer.WriteBuffer("Listening...");
HttpListenerContext context = listener.GetContext();
HttpListenerRequest request = context.Request;
// Obtain a response object.
HttpListenerResponse response = context.Response;
// Construct a response.
Writer.WriteBuffer("Context: " + context.ToString());
Writer.WriteBuffer("request: " + request.ToString());
Writer.WriteBuffer("response: " + response.ToString());

string responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
// Get a response stream and write the response to it.
response.ContentLength64 = buffer.Length;
System.IO.Stream output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
// You must close the output stream.
output.Close();
listener.Stop();

Writer.WriteBuffer is a wrapper for writing Console.Write essentially, just formats stuff in a much better way for me. I see the "listening..." come up, so great its listening.

Now, I copied an example from playfab and just adapted it very slightly. cloudscript is in js, so here is what I am running from playfab:

var headers ={"X-MyCustomHeader":"Some Value"};var body ={
    input: args,
    userId: currentPlayerId,
    mode:"foobar"};var url ="http://11.111.111.1/";var content = JSON.stringify(body);var httpMethod ="post";var contentType ="application/json";// The pre-defined http object makes synchronous HTTP requestsvar response = http.request(url, httpMethod, content, contentType, headers);return{ responseContent: response };

11.111.111.1 is where I put the IP address of the AWS instance (I've changed it for obvious reasons).

When running this, I get "httpRequestError": "Timeout". When I check on AWS, I have nothing else printed out other than "Listening...", so it hasn't handled anything yet.

I'm not too sure where the problem lies to be honest.

CloudScriptscheduled tasks
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
brendan avatar image
brendan answered

The port range that's open on the servers is 9xxx. We automatically assign them in order to the instances running on a server host, so the first one gets 9000, the second gets 9001, etc. So you could reasonably use the high end of that range for your custom communications.

However, I need to ask - why are you trying to message your server from Cloud Script? Why aren't you just calling the server from the client?

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.