Hello.
I have simple project on Unreal Engine 4 with plugin SocketIO (GitHub - getnamo/SocketIOClient-Unreal: Socket.IO client plugin for the Unreal Engine. and example here GitHub - getnamo/SocketIOClient-Unreal-Example: sample project using the socketio-client-ue4). If I used server from example (nodejs) I got answer from server in game, but I need create C# server and here I have big problem. It’s not easy as I thought.
I used next code of my c# server:
string ip = "127.0.0.1";
int port = 3000;
var server = new TcpListener(IPAddress.Parse(ip), port);
server.Start();
Console.WriteLine("Server has started on {0}:{1}, Waiting for a connection...", ip, port);
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("A client connected.");
NetworkStream stream = client.GetStream();
while (true)
{
while (!stream.DataAvailable) ;
byte[] bytes = new byte[client.Available];
stream.Read(bytes, 0, client.Available);
string s = Encoding.UTF8.GetString(bytes);
if (Regex.IsMatch(s, "^GET", RegexOptions.IgnoreCase))
{
string swk = Regex.Match(s, "Sec-WebSocket-Key: (.*)").Groups[1].Value.Trim();
string swka = swk + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
byte[] swkaSha1 = System.Security.Cryptography.SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(swka));
string swkaSha1Base64 = Convert.ToBase64String(swkaSha1);
byte[] response = Encoding.UTF8.GetBytes(
"HTTP/1.1 426 Upgrade Required\r\n" +
"Connection: Upgrade\r\n" +
"Upgrade: websocket\r\n" +
"Sec-WebSocket-Accept: " + swkaSha1Base64 + "\r\n\r\n");
stream.Write(response, 0, response.Length);
}
}
But I got request from game, but in game I didn’t get any result. When I started server and then the game, my server got request from game and in game I didn’t see any result. In next request from game to server, point all time in cycle:
while (!stream.DataAvailable) ;
Can someone help with this c# server code or show your part of c# server?