/// <summary> /// 发送 message 给 user /// </summary> /// <param name="oaClient">指定发给哪个用户</param> /// <param name="message">信息内容</param> private void _sendToClient(OaClient oaClient, string message) { try { //将字符串写入网络流,此方法会自动附加字符串长度前缀 oaClient.BinaryWriter.Write(message); oaClient.BinaryWriter.Flush(); } catch { //AddItemToListBox(string.Format("向[{0}]发送信息失败", oaClient.Name)); } }
/// <summary> /// 发送消息给所有客户端,不包括自己 /// </summary> /// <param name="oaClient">发送信息的客户端</param> /// <param name="message">信息内容</param> private void _sendToAllClient(OaClient oaClient, string message) { for (int i = 0; i < _oaClientList.Count; i++) { if (_oaClientList[i].Id != oaClient.Id) { _sendToClient(_oaClientList[i], message); } } }
/// <summary> /// 接收客户端连接 /// </summary> private void _listenClientConnect() { TcpClient tcpClient = null; while (true) { try { tcpClient = _tcpListener.AcceptTcpClient(); } catch { //当服务未启动时 AcceptTcpClient() 会产生异常 //因此可以利用此异常退出循环 continue; } OaClient oaClient = new OaClient(tcpClient); //每接收一个客户端连接,就创建一个对应的线程循环接收该客户端发来的信息; Thread threadReceive = new Thread(_receiveData); threadReceive.Start(oaClient); //AddItemToListBox(string.Format("[{0}]进入", newClient.Client.RemoteEndPoint)); //AddItemToListBox(string.Format("当前连接用户数:{0}", _oaClientList.Count)); } }
/// <summary> /// 移除用户 /// </summary> /// <param name="oaClient">指定要移除的用户</param> private void _removeUser(OaClient oaClient) { _oaClientList.Remove(oaClient); oaClient.Close(); }
private string _getMessages(OaClient oaClient) { var list = ctx.ExecuteQuery<FileUnread>( @"select a.Id, a.Name, a.Type, a.RealName, a.OwnerId, a.Pid, b.AuthDate, b.AuthLevel,d.Name as OriAuthor,e.Name as [From] from Files a inner join FileAuth b on a.Id=b.FileId and b.ToId={0} left join FileReadLog c on a.Id=c.FileId and c.ReaderId={0} left join Employee d on a.OwnerId=d.Id left join Employee e on b.FromId=e.Id where c.ReaderId is null order by b.AuthDate desc;", oaClient.Id).ToList(); string outJson = null; if (list.Any()) { outJson = JavaScriptConvert.SerializeObject(list); } return outJson; }