public async Task <long> 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. WebSocketConnection socket = WebSocketConnectionManager.GetSocketById(socketId); InvocationDescriptor invocationDescriptor = new InvocationDescriptor { MethodName = methodName, Params = methodParams }; if (socket == null) { return(-1); } invocationDescriptor.Id = socket.NextCmdId(); var message = new Message() { MessageType = MessageType.MethodInvocation, Data = JsonConvert.SerializeObject(invocationDescriptor) }; await SendMessageAsync(socketId, message).ConfigureAwait(false); return(invocationDescriptor.Id); }
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.WebSocket.State == WebSocketState.Open) { await CloseSocketAsync(socket, WebSocketCloseStatus.Empty, null, CancellationToken.None); } } else { if (socketPingMap[item.Key] > socketPongMap[item.Key]) { } await SendMessageAsync(item.Key, new Message() { Data = "ping", MessageType = MessageType.Text }); socketPingMap[item.Key] = DateTime.Now; } } } }
public async Task SendMessageAsync(string socketId, string text) { var message = new Message() { MessageType = MessageType.MethodInvocation, Data = text, }; await SendMessageAsync(WebSocketConnectionManager.GetSocketById(socketId), message).ConfigureAwait(false); }
public async Task SendClientNotifyAsync(string socketId, string methodName, object result) { // create the method invocation descriptor. InvocationDescriptor invocationDescriptor = new InvocationDescriptor { Id = 0, 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, long id, string methodName, RemoteException error) { // create the method invocation descriptor. InvocationResult invocationResult = new InvocationResult { Id = id, MethodName = methodName, Exception = error }; WebSocketConnection socket = WebSocketConnectionManager.GetSocketById(socketId); if (socket == null) { return; } var message = new Message() { MessageType = MessageType.MethodInvocation, Data = JsonConvert.SerializeObject(invocationResult) }; await SendMessageAsync(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. }
public async Task SendMessageAsync(string socketId, Message message) { await SendMessageAsync(WebSocketConnectionManager.GetSocketById(socketId), message).ConfigureAwait(false); }