private static Task SendToClientWebSocket(WebSocket ws, ReceiveSocketDataModel data, CancellationToken cancellation) { var dataJson = JsonConvert.SerializeObject(data); var encoded = Encoding.UTF8.GetBytes(dataJson); var buffer = new ArraySegment <Byte>(encoded, 0, encoded.Length); return(ws.SendAsync(buffer, WebSocketMessageType.Text, true, cancellation)); }
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)); }
public async Task SendMessageToId(string text, string channelSlugUrl, string connectionId) { var data = new ReceiveSocketDataModel { ConnectionId = connectionId, MessageJson = new[] { text }, InvokeMethodName = "SendMessageToId", //System.Reflection.MethodBase.GetCurrentMethod().Name }; await EchoProcess.SendToClientConnectionId(channelSlugUrl, data); }
private static async Task FlushAllConnectionList(string channelSlugUrl) { #region FlushAllConnectionList var allConnectionList = RegisWebSocketProcess.GetConnectionRegisListFromSlug(channelSlugUrl); foreach (var connection in allConnectionList) { var FlushAllConnectionList = new ReceiveSocketDataModel { ConnectionId = connection.ConnectionId, ConnectionName = "", MessageJson = new[] { JsonConvert.SerializeObject(allConnectionList.Select(s => s.ConnectionId)) }, InvokeMethodName = "FlushAllConnectionList" }; await SendToClientWebSocket(connection.WebSocket, FlushAllConnectionList, CancellationToken.None); } #endregion }
//[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] //public string GetCurrentMethod() //{ // System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(); // System.Diagnostics.StackFrame sf = st.GetFrame(1); // return sf.GetMethod().Name; //} public override async Task OnConnectedAsync() { //var methodName = new StackTrace().GetFrame(2).GetMethod().Name; var methodName = nameof(this.OnConnectedAsync); #region Reply Connecttion Id var replyConnectionData = new ReceiveSocketDataModel { ConnectionId = Context.ConnectionId, ConnectionName = "", //todo set MessageJson = new[] { RegisWebSocketProcess.GetConnectionRegisListFromSlug(Context.ChannelSlugUrl) }, InvokeMethodName = methodName }; await EchoProcess.SendToClientConnectionId(Context.ChannelSlugUrl, replyConnectionData); #endregion //todo flush this to any client }
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); }
public static async Task <object> InvokeMethod(ConnectionSocketDataModel userConnectionData, ReceiveSocketDataModel receiveDataModel) { try { var channel = GetChannelList().Where(w => w.ChannelSlugUrl == userConnectionData.ChannelSlugUrl).FirstOrDefault(); var mainTypeData = Type.GetType(channel.ChannelClassFullName); var mainParamConstructor = SummonParameter(mainTypeData); var mainConstructor = mainTypeData.GetConstructors().FirstOrDefault(); var mainConstructorDeclare = mainConstructor.Invoke(mainParamConstructor); #region SetPropertieValueAfterNewInstance var hubContext = mainTypeData.GetProperty(nameof(Hub.Context)); var hubContextInstance = Activator.CreateInstance(hubContext.PropertyType, userConnectionData.ConnectionId, userConnectionData.ChannelSlugUrl); //mainConstructorDeclare is same a class varaible new instance hubContext.SetValue(mainConstructorDeclare, hubContextInstance as HubContext); #endregion var mainMethod = mainTypeData.GetMethod(receiveDataModel.InvokeMethodName); var IsNotReturn = (mainMethod.ReturnType == typeof(void) || mainMethod.ReturnType == typeof(Task)); object response = null; if (IsAsyncMethod(mainMethod, receiveDataModel.InvokeMethodName)) { // check invoke async dynamic invokeAsync = mainMethod.Invoke(mainConstructorDeclare, receiveDataModel.MessageJson); if (IsNotReturn) { await invokeAsync; } else { response = await invokeAsync; } } else { if (IsNotReturn) { mainMethod.Invoke(mainConstructorDeclare, receiveDataModel.MessageJson); } else { response = mainMethod.Invoke(mainConstructorDeclare, receiveDataModel.MessageJson); } } return(response); } catch (Exception e) { //TODO : log return(e); } }