/// <summary> /// 调用远程端实现的服务方法 /// </summary> /// <param name="api">api</param> /// <param name="parameters">参数列表</param> /// <exception cref="SocketException"></exception> /// <exception cref="SerializerException"></exception> public void InvokeApi(string api, params object[] parameters) { var packet = new FastPacket { api = api, id = this.packetIdProvider.GetId(), state = true, fromClient = false, body = parameters }; var packetJson = this.JsonSerializer.Serialize(packet); this.SendText(packetJson); }
/// <summary> /// 获取数据包 /// </summary> /// <param name="client">客户端</param> /// <param name="content">内容</param> /// <returns></returns> private FastPacket GetFastPacket(FastWebSocketSession client, string content) { try { dynamic packet = JObject.Parse(content); var fastPacket = new FastPacket { api = packet.api, id = packet.id ?? 0, state = packet.state ?? true, fromClient = packet.fromClient ?? true, body = packet.body }; return(fastPacket); } catch (Exception) { client.Close(StatusCodes.ProtocolError); return(null); } }
/// <summary> /// 调用客户端实现的服务方法 /// 并返回结果数据任务 /// </summary> /// <typeparam name="T">返回值类型</typeparam> /// <param name="api">api</param> /// <param name="parameters">参数</param> /// <exception cref="SocketException"></exception> /// <exception cref="SerializerException"></exception> /// <returns>远程数据任务</returns> public Task <T> InvokeApi <T>(string api, params object[] parameters) { var packet = new FastPacket { api = api, id = this.Server.PacketIdProvider.NewId(), state = true, fromClient = false, body = parameters }; // 登记TaskSetAction var taskSource = new TaskCompletionSource <T>(); var taskSetAction = new TaskSetAction <T>(taskSource); this.Server.TaskSetActionTable.Add(packet.id, taskSetAction); var packetJson = this.Server.JsonSerializer.Serialize(packet); this.SendText(packetJson); return(taskSource.Task); }
/// <summary> /// 请求上下文 /// </summary> /// <param name="session">当前会话对象</param> /// <param name="packet">数据包对象</param> /// <param name="allSessions">所有会话对象</param> internal RequestContext(FastWebSocketSession session, FastPacket packet, IEnumerable<FastWebSocketSession> allSessions) { this.Session = session; this.Packet = packet; this.AllSessions = allSessions; }
/// <summary> /// 请求上下文 /// </summary> /// <param name="session">当前会话对象</param> /// <param name="packet">数据包对象</param> /// <param name="allSessions">所有会话对象</param> internal RequestContext(FastWebSocketSession session, FastPacket packet, IEnumerable <FastWebSocketSession> allSessions) { this.Session = session; this.Packet = packet; this.AllSessions = allSessions; }