Esempio n. 1
0
        public static Task SendToClientConnectionId(string channelSlugUrl, ReceiveSocketDataModel receivedData, CancellationToken?cancellation = null)
        {
            var dataJson           = JsonConvert.SerializeObject(receivedData);
            var tarGetConnectionId = RegisWebSocketProcess.GetConnectionRegis(receivedData.ConnectionId, channelSlugUrl);
            var encoded            = Encoding.UTF8.GetBytes(dataJson);
            var buffer             = new ArraySegment <Byte>(encoded, 0, encoded.Length);

            return(tarGetConnectionId.WebSocket.SendAsync(buffer, WebSocketMessageType.Text, true, cancellation ?? CancellationToken.None));
        }
Esempio n. 2
0
        public static async Task Echo(HttpContext context, WebSocket webSocket)
        {
            var channelSlugUrl = context.Request.Path.Value.Substring(1);
            var connectionId   = context.Connection.Id;
            //regis user subscript websocket  Connection
            var userConnectionData =
                RegisWebSocketProcess.GetConnectionRegis(connectionId, channelSlugUrl)
                ??
                RegisWebSocketProcess.RegisWebSocket(channelSlugUrl, connectionId, webSocket);

            ////#region Reply Connecttion Id
            ////var replyConnectionData = new ReceiveSocketDataModel
            ////{
            ////    ConnectionId = userConnectionData.ConnectionId,
            ////    ConnectionName = "",
            ////    MessageJson = new[] { "connection", "successful" },
            ////    InvokeMethodName = "InitialConnection"
            ////};
            ////await SendToWebSocket(webSocket, replyConnectionData, CancellationToken.None);
            ////#endregion
            ////SetConnectionSocketList(userConnectionData);


            //await FlushAllConnectionList(channelSlugUrl);

            var buffer = new byte[1024 * 4];
            var result = await webSocket.ReceiveAsync(new ArraySegment <byte>(buffer), CancellationToken.None);

            while (!result.CloseStatus.HasValue)
            {
                if (result.MessageType == WebSocketMessageType.Text)
                {
                    //var chanelList = GetChannelList();
                    var content          = Encoding.UTF8.GetString(buffer, 0, result.Count);
                    var receiveDataModel = JsonConvert.DeserializeObject <ReceiveSocketDataModel>(content);

                    #region UseWhenInvoke method and return data to client
                    // var typeVar = Type.GetType("SignalService.MyTestHub");
                    // TODO : "InvokeMethod
                    var invokeResult = await InvokeProcess.InvokeMethod(userConnectionData, receiveDataModel);

                    //TODO : get this varaible [invokeResult] type  check before SendString()  // //  // Convert.ChangeType(mainValue, mainMethod.ReturnType) ;
                    if (invokeResult != null)
                    {
                        var invokeResultString = invokeResult as string;
                        var replySubscriptData = new ReceiveSocketDataModel
                        {
                            ConnectionId     = receiveDataModel.ConnectionId,
                            ConnectionName   = receiveDataModel.ConnectionName,
                            MessageJson      = new[] { invokeResultString },
                            InvokeMethodName = receiveDataModel.InvokeMethodName
                        };
                        await SendToClientWebSocket(webSocket, replySubscriptData, CancellationToken.None);
                    }
                    // --//
                    #endregion
                }
                //await webSocket.SendAsync(new ArraySegment<byte>(buffer, 0, result.Count), result.MessageType, result.EndOfMessage, CancellationToken.None);

                result = await webSocket.ReceiveAsync(new ArraySegment <byte>(buffer), CancellationToken.None);
            }
            await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);

            RegisWebSocketProcess.RemoveConnectionSocket(userConnectionData.ConnectionId, channelSlugUrl);
            //await FlushAllConnectionList(channelSlugUrl);
        }