Esempio n. 1
0
        //private static string getStrFromByte(ref byte[] buffer)
        //{
        //    string str = "";

        //    for (var i = buffer.Length - 1; i >= 0; i--)
        //    {
        //        if (buffer[i] != 0)
        //        {
        //            str = Encoding.UTF8.GetString(buffer.Take(i + 1).ToArray()).Trim();
        //            break;
        //        }

        //    }
        //    buffer = new byte[size];
        //    return str;

        //}

        private static void notify(IApplicationBuilder app)
        {
            app.Run(async context =>
            {
                if (context.WebSockets.IsWebSocketRequest)
                {
                    var webSocket = await context.WebSockets.AcceptWebSocketAsync();
                    await dealWithNotify(webSocket);
                }
            });
            return;

            app.Run(async context =>
            {
                if (context.Request.Method.ToLower() == "post")
                {
                    var notifyJson = getBodyStr(context);
                    //  await context.Response.WriteAsync("ok");

                    //var notifyJson = getBodyStr(context);

                    Console.WriteLine($"notify receive:{notifyJson}");
                    CommonClass.CommandNotify c = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.CommandNotify>(notifyJson);

                    // CommonClass.TeamCreateFinish teamCreateFinish = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonClass.TeamCreateFinish>(notifyJson);

                    WebSocket ws = null;
                    lock (ConnectInfo.connectedWs_LockObj)
                    {
                        if (ConnectInfo.connectedWs.ContainsKey(c.WebSocketID))
                        {
                            if (ConnectInfo.connectedWs[c.WebSocketID].State == WebSocketState.Open)
                            {
                                ws = ConnectInfo.connectedWs[c.WebSocketID];
                            }
                            else
                            {
                                ConnectInfo.connectedWs.Remove(c.WebSocketID);
                            }
                        }
                    }
                    await context.Response.WriteAsync("ok");
                    if (ws != null)
                    {
                        if (ws.State == WebSocketState.Open)
                        {
                            try
                            {
                                var sendData = Encoding.UTF8.GetBytes(notifyJson);
                                await ws.SendAsync(new ArraySegment <byte>(sendData, 0, sendData.Length), WebSocketMessageType.Text, true, CancellationToken.None);
                            }
                            catch
                            {
                            }
                        }
                    }
                }
            });
        }
Esempio n. 2
0
        //private async void startTcp()
        async Task StartTcpDealWithF(string notifyJson)
        {
            try
            {
                CommonClass.CommandNotify c = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.CommandNotify>(notifyJson);
                WebSocket ws = null;
                lock (ConnectInfo.connectedWs_LockObj)
                {
                    if (ConnectInfo.connectedWs.ContainsKey(c.WebSocketID))
                    {
                        if (!ConnectInfo.connectedWs[c.WebSocketID].CloseStatus.HasValue)
                        {
                            ws = ConnectInfo.connectedWs[c.WebSocketID];
                        }
                        else
                        {
                            ConnectInfo.connectedWs.Remove(c.WebSocketID);
                        }
                    }
                }
                // await context.Response.WriteAsync("ok");
                if (ws != null)
                {
                    if (ws.State == WebSocketState.Open)
                    {
                        try
                        {
                            var sendData = Encoding.UTF8.GetBytes(notifyJson);

                            await ws.SendAsync(new ArraySegment <byte>(sendData, 0, sendData.Length), WebSocketMessageType.Text, true, CancellationToken.None);
                        }
                        catch
                        {
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Esempio n. 3
0
        //private static Task dealWithNotify(WebSocket webSocket)
        //{
        //    throw new NotImplementedException();
        //}
        private static async Task dealWithNotify(System.Net.WebSockets.WebSocket webSocketFromGameRoom)
        {
            throw new Exception("");
            WebSocketReceiveResult wResult;

            {
                //  do
                {
                    try
                    {
                        var returnResult = await ReceiveStringAsync(webSocketFromGameRoom, webWsSize);

                        wResult = returnResult.wr;
                        // Console.WriteLine($"receive:{returnResult.result}");

                        var notifyJson = returnResult.result;
                        Console.WriteLine($"notify receive:{notifyJson}");
                        CommonClass.CommandNotify c = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.CommandNotify>(notifyJson);

                        // CommonClass.TeamCreateFinish teamCreateFinish = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonClass.TeamCreateFinish>(notifyJson);

                        WebSocket ws = null;
                        lock (ConnectInfo.connectedWs_LockObj)
                        {
                            if (ConnectInfo.connectedWs.ContainsKey(c.WebSocketID))
                            {
                                if (ConnectInfo.connectedWs[c.WebSocketID].State == WebSocketState.Open)
                                {
                                    ws = ConnectInfo.connectedWs[c.WebSocketID];
                                }
                                else
                                {
                                    ConnectInfo.connectedWs.Remove(c.WebSocketID);
                                }
                            }
                        }
                        // await context.Response.WriteAsync("ok");
                        if (ws != null)
                        {
                            if (ws.State == WebSocketState.Open)
                            {
                                try
                                {
                                    var sendData = Encoding.UTF8.GetBytes(notifyJson);
                                    await ws.SendAsync(new ArraySegment <byte>(sendData, 0, sendData.Length), WebSocketMessageType.Text, true, CancellationToken.None);

                                    await ws.CloseAsync(WebSocketCloseStatus.Empty, null, CancellationToken.None);
                                }
                                catch
                                {
                                }
                            }
                        }
                    }
                    catch
                    {
                        // Console.WriteLine($"step2:webSockets数量:{   BufferImage.webSockets.Count}");
                        return;
                    }
                }
                //while (!wResult.CloseStatus.HasValue);
                //try
                //{
                //    await webSocketFromGameRoom.CloseAsync(wResult.CloseStatus.Value, wResult.CloseStatusDescription, CancellationToken.None);


                //}
                //catch
                //{
                //    return;
                //}
            };
        }