WEB开发网
开发学院图形图像Flash 使用SilverLight构建插件式应用程序(八) —聊天插... 阅读

使用SilverLight构建插件式应用程序(八) —聊天插件Duplex WCF的实现

 2008-10-23 11:43:33 来源:WEB开发网   
核心提示: 这个WCF服务并没有从接口继承过来,而是直接地实现,使用SilverLight构建插件式应用程序(八) —聊天插件Duplex WCF的实现(2),这个和MSDN上边稍微有点差异,之所以这样做,这就是说客户端掉线或者其他原因了,5:用户进入聊天室://进入聊天室[OperationCont

这个WCF服务并没有从接口继承过来,而是直接地实现,这个和MSDN上边稍微有点差异,之所以这样做,因为将来可以使用Forms验证模式来验证用户,这是一个主要的原因,将来说到权限的时候,我会具体的说到这一点。

4: 接受心跳信息:

  /// <summary>
  /// 获取心跳信息更新连接
  /// </summary>
  /// <param name="receivedMessage"></param>
  [OperationContract(IsOneWay = true)]
  public void Heart(Message receivedMessage)
  {
    ChatMessage chatMessage = receivedMessage.GetBody<ChatMessage>();
    IWSChatClient chatclient = OperationContext.Current.GetCallbackChannel<IWSChatClient>();
    if (chatClients.ContainsKey(chatMessage.From))
    {
      chatClients[chatMessage.From] = chatclient;
      //
      chatMessage.From = "SystemServer";
      chatMessage.To = chatMessage.From;
      chatMessage.Type = -2;
      Message returnMessage = Message.CreateMessage(MessageVersion.Soap11, "WindCloud/WSChatService/Receive", chatMessage);
      chatclient.Receive(returnMessage);
    }
  }

客户端会定时发送心跳信息到服务器端,如果客户端有段时间没有发送心跳信息,这就是说客户端掉线或者其他原因了。

5:用户进入聊天室:

//进入聊天室
  [OperationContract(IsOneWay = true)]
  public void Join(Message receivedMessage)
  {
    bool userAdded = false;
    Message returnMessage;
    ChatMessage chatMessage = receivedMessage.GetBody<ChatMessage>();
    IWSChatClient chatclient = OperationContext.Current.GetCallbackChannel<IWSChatClient>();
    ChatMessage sendChatMessage = new ChatMessage();
    sendChatMessage.From = "Server:";
    sendChatMessage.Data = "Service:请等待...";
    sendChatMessage.Type = -1;
    //
    lock (syncObj)
    {    
      if (!chatClients.ContainsKey(chatMessage.From) && chatMessage.From != "" && chatMessage.From != null)
      {
        chatMessage.Data = "Service:开始添加用户...";
        returnMessage = Message.CreateMessage(MessageVersion.Soap11, "WindCloud/WSChatService/Receive", chatMessage);
        chatclient.Receive(returnMessage);
        this.name = chatMessage.From;
        m_NameList.Add(chatMessage.From);
        chatClients.Add(chatMessage.From, chatclient);
        userAdded = true;
      }
      else
      {
        ChatMessage cm = new ChatMessage();
        cm.From = "Server:";
        cm.Data = "Service:User:" + chatMessage + "加入失败,用户已存在。";
        returnMessage = Message.CreateMessage(MessageVersion.Soap11, "WindCloud/WSChatService/Receive", cm);
        //
        chatclient.Receive(returnMessage);
        return;
      }
      //发送留言
      ChatBLL bll = new ChatBLL();
      List<ChatMessage> cms = bll.GetChats(chatMessage.From);
      foreach (ChatMessage cm in cms)
      {
        SendMessageToClient(cm);
      }
      //删除留言
      bll.Delete(chatMessage.From);
    }
    if (userAdded)
    {
      string text2 = "Service:User:" + chatMessage + " is already Joined";
      ChatMessage cm = new ChatMessage();
      cm.From = "Server:";
      cm.Data = text2;
      SendMessageToClient(cm);
    }
  }

上一页  1 2 3 4  下一页

Tags:使用 SilverLight 构建

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接