public async Task InvokeClientMethodAsync(string socketId, string methodName, object[] arguments) { object methodParams = null; if (arguments.Length == 1) { methodParams = arguments[0]; } else { methodParams = arguments; } // create the method invocation descriptor. InvocationDescriptor invocationDescriptor = new InvocationDescriptor { MethodName = methodName, Params = methodParams }; WebSocketConnection socket = WebSocketConnectionManager.GetSocketById(socketId); if (socket == null) { return; } invocationDescriptor.Id = socket.NextCmdId(); var message = new Message() { MessageType = MessageType.MethodInvocation, Data = JsonConvert.SerializeObject(invocationDescriptor) }; await SendMessageAsync(socketId, message).ConfigureAwait(false); }
private async void OnPingTimer(object state) { if (SendPingMessages) { TimeSpan timeoutPeriod = TimeSpan.FromSeconds(WebSocket.DefaultKeepAliveInterval.TotalSeconds * 3); foreach (var item in socketPongMap) { if (item.Value < DateTime.Now.Subtract(timeoutPeriod)) { var socket = WebSocketConnectionManager.GetSocketById(item.Key); if (socket.State == WebSocketState.Open) { logger.LogInformation("Closing socket due to ping no ping response. LastPongResponseTime: {LastPongResponseTime}", item.Value); await CloseSocketAsync(socket, WebSocketCloseStatus.Empty, null, CancellationToken.None); } } else { if (socketPingMap[item.Key] > socketPongMap[item.Key]) { logger.LogDebug("Sending ping without receiving corresponding pong"); } await SendMessageAsync(item.Key, new Message() { Data = "ping", MessageType = MessageType.Text, Brief = "ping" }); socketPingMap[item.Key] = DateTime.Now; logger.LogDebug("Sending websocket ping"); } } } }
public async Task SendClientNotifyAsync(string socketId, string methodName, object result) { // create the method invocation descriptor. InvocationDescriptor invocationDescriptor = new InvocationDescriptor { MethodName = methodName, Params = result }; WebSocketConnection socket = WebSocketConnectionManager.GetSocketById(socketId); if (socket == null) { return; } var message = new Message() { MessageType = MessageType.MethodInvocation, Data = JsonConvert.SerializeObject(invocationDescriptor) }; await SendMessageAsync(socketId, message).ConfigureAwait(false); }
public async Task SendClientErrorAsync(string socketId, string methodName, RemoteException error) { // create the method invocation descriptor. InvocationResult invocationResult = new InvocationResult { MethodName = methodName, Exception = error }; WebSocketConnection socket = WebSocketConnectionManager.GetSocketById(socketId); if (socket == null) { return; } invocationResult.Id = socket.NextCmdId(); var message = new Message() { MessageType = MessageType.MethodInvocation, Data = JsonConvert.SerializeObject(invocationResult) }; await SendMessageAsync(socketId, message).ConfigureAwait(false); }
public async Task SendMessageAsync(string socketId, Message message) { await SendMessageAsync(WebSocketConnectionManager.GetSocketById(socketId), message).ConfigureAwait(false); }
public async Task <T> InvokeClientMethodAsync <T>(string socketId, string methodName, object[] arguments) { // create the method invocation descriptor. object methodParams = null; if (arguments.Length == 1) { methodParams = arguments[0]; } else { methodParams = arguments; } InvocationDescriptor invocationDescriptor = new InvocationDescriptor { MethodName = methodName, Params = methodParams }; WebSocketConnection socket = WebSocketConnectionManager.GetSocketById(socketId); // generate a unique identifier for this invocation. if (socket == null) { return(default(T)); } invocationDescriptor.Id = socket.NextCmdId(); // Guid.NewGuid(); // add ourselves to the waiting list for return values. TaskCompletionSource <InvocationResult> task = new TaskCompletionSource <InvocationResult>(); // after a timeout of 60 seconds we will cancel the task and remove it from the waiting list. new CancellationTokenSource(1000 * 60).Token.Register(() => { _waitingRemoteInvocations[socketId].Remove(invocationDescriptor.Id); task.TrySetCanceled(); }); if (!_waitingRemoteInvocations.ContainsKey(socketId)) { _waitingRemoteInvocations[socketId] = new Dictionary <long, TaskCompletionSource <InvocationResult> >(); } _waitingRemoteInvocations[socketId].Add(invocationDescriptor.Id, task); // send the method invocation to the client. var message = new Message() { MessageType = MessageType.MethodInvocation, //Data = JsonConvert.SerializeObject(invocationDescriptor, _jsonSerializerSettings) Data = JsonConvert.SerializeObject(invocationDescriptor) }; await SendMessageAsync(socketId, message).ConfigureAwait(false); // wait for the return value elsewhere in the program. InvocationResult result = await task.Task; // ... we just got an answer. // if we have completed successfully: if (task.Task.IsCompleted) { // there was a remote exception so we throw it here. if (result.Exception != null) { throw new Exception(result.Exception.Message); } // return the value. // support null. if (result.Result == null) { return(default(T)); } // cast anything to T and hope it works. return((T)result.Result); } // if we reach here we got cancelled or alike so throw a timeout exception. throw new TimeoutException(); // todo: insert fancy message here. }