예제 #1
0
        public void SendAsClient(Server server, IMessenger msgr, BaseTransportMessage msg)
        {
            var ctx = new WebSocketServerContext(server);
            var l   = server.ServerContext.L;

            Task.Run(() =>
            {
                l.Log("Establishing connection to local socket server.");
                using (var ws = new WebSocketSharp.WebSocket($"ws://{IPAddress.Loopback}:{ctx.Port}/"))
                {
                    var settings = new JsonSerializerSettings()
                    {
                        TypeNameHandling    = TypeNameHandling.Objects,
                        SerializationBinder = server.TypeBinder
                    };

                    var helper = new MessageTypedSender(C.M);

                    ws.OnMessage += (sender, args) =>
                    {
                        if (args.IsText)
                        {
                            var dataText = args.Data;
                            if (string.IsNullOrWhiteSpace(dataText))
                            {
                                return;
                            }

                            if (JsonConvert.DeserializeObject(dataText, settings) is BaseTransportMessage m)
                            {
                                helper.UnPackSendReceivedMessage(new ExternalMessage(m.SessionId, m));
                            }
                        }
                        else
                        {
                            throw new InvalidOperationException("Only text data is supported.");
                        }
                    };

                    ws.Connect();

                    var json = JsonConvert.SerializeObject(new HelloRequest(), settings);

                    ws.Send(json);

                    while (ws.IsAlive)
                    {
                    }
                }
            });
        }
예제 #2
0
        public void SendAsClient(Server server, IMessenger msgr, BaseTransportMessage msg, bool echoClient = false)
        {
            var ctx = new WebSocketServerContext(server);
            var l   = server.ServerContext.L;

            Task.Run(() =>
            {
                l.Log("Establishing connection to local socket server.");
                using (var ws = new WebSocketSharp.WebSocket($"ws://{IPAddress.Loopback}:{ctx.Port}/{(echoClient ? "echo": "")}"))
                {
                    var settings = new JsonSerializerSettings()
                    {
                        TypeNameHandling    = TypeNameHandling.Objects,
                        SerializationBinder = server.TypeBinder
                    };

                    var helper = new MessageTypedSender(C.M);

                    ws.OnMessage += (sender, args) =>
                    {
                        if (args.IsText)
                        {
                            var dataText = args.Data;
                            if (string.IsNullOrWhiteSpace(dataText))
                            {
                                return;
                            }

                            l.Log($"Raw from server: {dataText}");

                            if (JsonConvert.DeserializeObject(dataText, settings) is BaseTransportMessage m)
                            {
                                helper.UnPackSendReceivedMessage(new ExternalMessage(m.SessionId, m));
                            }
                        }
                        else
                        {
                            throw new InvalidOperationException("Only text data is supported.");
                        }
                    };

                    ws.Connect();

                    var json = JsonConvert.SerializeObject(new HelloRequest(), settings);

                    ws.Send(json);

                    while (ws.IsAlive)
                    {
                    }
                }
            });

            //var settings = new JsonSerializerSettings()
            //{
            //    TypeNameHandling = TypeNameHandling.Objects,
            //    SerializationBinder = server.TypeBinder
            //};

            //var dataString = JsonConvert.SerializeObject(msg, settings);

            //l.Log("Connection established, sending message: " + dataString);

            //var dataBytes = dataString.GetBytes();

            //client.Send(dataBytes);

            //Task.Run(() =>
            //{
            //    var helper = new MessageTypedSender(C.M);

            //    do
            //    {
            //        var buffer = new byte[1024];
            //        var iRx = client.Receive(buffer);
            //        var recv = buffer.GetString().Substring(0, iRx);

            //        if (string.IsNullOrWhiteSpace(recv))
            //            continue;

            //        if (JsonConvert.DeserializeObject(recv, settings) is BaseTransportMessage m)
            //            helper.UnPackSendReceivedMessage(new ExternalMessage(m.ClientId, m));

            //    } while (client.Connected);
            //});
        }