static void WebServerOnConnect(int id) { _connectedIds.Add(id); _playerDatas[id] = new PlayerData() { id = (ushort)id }; // Tell new client their id and the game state and everyone's name and points _bitBuffer.Clear(); _bitBuffer.AddByte(2); _bitBuffer.AddUShort((ushort)id); _bitBuffer.AddByte((byte)_currentState); _bitBuffer.AddUShort((ushort)_playerDatas.Count); foreach (var playerData in _playerDatas.Values) { _bitBuffer.AddUShort(playerData.id); _bitBuffer.AddString(playerData.name); _bitBuffer.AddShort(playerData.points); } _bitBuffer.ToArray(_buffer); _webServer.SendOne(id, new ArraySegment <byte>(_buffer, 0, 5 + _playerDatas.Count * 20)); }
static void WebServerOnConnect(int id) { _connectedIds.Add(id); _playerDatas[id] = new PlayerData() { id = (ushort)id }; _bitBuffer.ToArray(_buffer); _webServer.SendOne(id, new ArraySegment <byte>(_buffer, 0, 5 + _playerDatas.Count * 20)); }
static void Main(string[] args) { SslConfig sslConfig; TcpConfig tcpConfig = new TcpConfig(true, 5000, 20000); Console.WriteLine("Setting up secure server"); sslConfig = new SslConfig(true, "cert.pfx", "", SslProtocols.Tls12); _webServer = new SimpleWebServer(10000, tcpConfig, 16 * 1024, 3000, sslConfig); _webServer.Start(Constants.GAME_PORT); Console.WriteLine("Server started"); _webServer.onConnect += delegate(int i) { Console.WriteLine($"Client {i} joined, sending hello"); byte[] buff = System.Text.Encoding.UTF8.GetBytes("Hello from server"); _webServer.SendOne(i, new ArraySegment <byte>(buff)); }; _webServer.onData += delegate(int i, ArraySegment <byte> data) { string msg = System.Text.Encoding.UTF8.GetString(data.Array); Console.WriteLine($"Client {i} send: {msg}"); }; while (!Console.KeyAvailable) { _webServer.ProcessMessageQueue(); } Console.WriteLine("Closing server"); _webServer.Stop(); }
private void WebServerOnonConnect(int id) { _connectionIds.Add(id); _playerTransforms[(ushort)id] = null; _bitBuffer.Clear(); _bitBuffer.AddUShort(2); // id notification message _bitBuffer.AddUShort((ushort)id); _bitBuffer.ToArray(_smallBuffer); _webServer.SendOne(id, new ArraySegment <byte>(_smallBuffer, 0, 4)); }
static void WebServerOnConnect(int id) { _connectedIds.Add(id); _playerDatas[id] = new PlayerData(); // Tell new client their id _bitBuffer.Clear(); _bitBuffer.AddByte(2); _bitBuffer.AddUShort((ushort)id); _bitBuffer.ToArray(_buffer); _webServer.SendOne(id, new ArraySegment <byte>(_buffer, 0, 3)); }
IEnumerator Send(int size) { byte[] bytes = new byte[size]; var random = new System.Random(); random.NextBytes(bytes); var segment = new ArraySegment <byte>(bytes); sent.Add(size, bytes); server.SendOne(1, segment); yield return(new WaitForSeconds(0.5f)); }
static void WebServerOnConnect(int id) { _connectedIds.Add(id); _playerDatas[id] = new PlayerData() { id = (ushort)id }; // Tell new client their id and the game state and everyone's name _bitBuffer.Clear(); _bitBuffer.AddByte(2); _bitBuffer.AddUShort((ushort)id); _bitBuffer.AddByte((byte)_currentState); _bitBuffer.ToArray(_buffer); _webServer.SendOne(id, new ArraySegment <byte>(_buffer, 0, 3)); }
static void Main(string[] args) { SslConfig sslConfig; TcpConfig tcpConfig = new TcpConfig(true, 5000, 20000); Console.WriteLine("Setting up secure server"); sslConfig = new SslConfig(true, "cert.pfx", "", SslProtocols.Tls12); _webServer = new SimpleWebServer(10000, tcpConfig, 16 * 1024, 3000, sslConfig); _webServer.Start(Constants.GAME_PORT); Console.WriteLine("Server started"); _webServer.onConnect += delegate(int i) { Console.WriteLine($"Client {i} joined, sending hello"); _webServer.SendOne(i, "Hello from server"); }; }
private async Task ServerServer() { try { // create server instance var tcpConfig = new TcpConfig(noDelay: false, sendTimeout: 5000, receiveTimeout: 20000); var server = new SimpleWebServer(5000, tcpConfig, ushort.MaxValue, 5000, new SslConfig()); // listen for events server.onConnect += (id) => Debug.Log($"New Client connected, id:{id}"); server.onDisconnect += (id) => Debug.Log($"Client disconnected, id:{id}"); server.onData += (id, data) => { Debug.Log($"Data from Client, id:{id}, {BitConverter.ToString(data.Array, data.Offset, data.Count)}"); // pong server.SendOne(id, data); }; server.onError += (id, exception) => Debug.Log($"Error because of Client, id:{id}, Error:{exception}"); // start server listening on port 7777 server.Start(7777); while (true) { server.ProcessMessageQueue(); await Task.Yield(); if (token.stop) { break; } } token = null; } catch (Exception e) { Debug.LogException(e); } }