SuperSocket+unity 网络笔记

学习SuperSocket 必须要注意的 代码是

 static void Main(string[] args)
        {   
            WebSocketServer appServer = new WebSocketServer();
            
            if (!appServer.Setup(2000)) //端口
            {
                Console.WriteLine("端口错误!");
                Console.ReadKey();
                return;
            }

            appServer.NewSessionConnected += new SessionHandler<WebSocketSession>(appServer_NewClientConnected); 
            appServer.NewMessageReceived += new SessionHandler<WebSocketSession, string>(appServer_NewMessageReceived); 
            appServer.SessionClosed += new SessionHandler<WebSocketSession, CloseReason>(appServer_SessionClosed); 

            if (!appServer.Start())
            {
                Console.WriteLine("启动错误");
                Console.ReadKey();
                return;
            }

            Console.WriteLine("服务器启动成功, 按 'q' 退出服务器!");
            while (Console.ReadKey().KeyChar != 'q')
            {
                Console.WriteLine();
                continue;
            } 
            appServer.Stop();
            Console.WriteLine();
            Console.WriteLine("服务器关闭");
            Console.ReadKey();
        }

学习 unity 通讯 必须注意的代码是

       // 服务器IP地址和端口号
        ServerAdress = "ws://192.168.1.104:2000/"; 
        websocket = new WebSocket(ServerAdress);
        websocket.Open();
    public void addlistenerSocket()
    { 
        _SocketModel.websocket.Opened += new EventHandler(websocket_Opened);
        _SocketModel.websocket.Error += new EventHandler<ErrorEventArgs>(websocket_Error);  
        _SocketModel.websocket.Closed += new EventHandler(websocket_Closed);
        _SocketModel.websocket.MessageReceived += new EventHandler<MessageReceivedEventArgs>(websocket_MessageReceived); 
    } 
 //登录 发送监听
    private void websocket_Opened(object sender, EventArgs e)
    { 
       JsonData message = new JsonData(); 
       message["Agreement"] = Agreement.user_no_return; 
       message["username"] = _SocketModel.PlayName;  
       _SocketModel.websocket.Send(message.ToJson());//这个是发到消息端
    }

    //关闭监听
    public virtual void websocket_Closed(object sender, EventArgs e)
    {
    }
    //接收监听
    public void websocket_MessageReceived(object sender, MessageReceivedEventArgs e)
    { 
        JsonData jsontext = JsonMapper.ToObject(e.Message);  //转化成json格式数据    
        string Agreements = jsontext["Agreement"].ToString();
        switch (Agreements)
        {
            case Agreement.user_offline_return://接收到用户下线通知 
                user_offline_return(jsontext);
                break;
            case Agreement.user_chat_return://接收聊天信息 
                user_chat_return(jsontext);
                break;
            case Agreement.user_battle_return://接收到对战通知
                user_battle_return(jsontext);
                break;
            case Agreement.user_information_return://接收到对方用户信息
                user_information_return(jsontext);
                break;
            case Agreement.user_trading_return://接收到用户交易信息
                user_trading_return(jsontext);
                break;
            case Agreement.user_trading_cancel_return://接收到用户交易信息取消
                user_trading_cancel_return(jsontext);
                break;
            case Agreement.user_trading_confirm_one_return://接收到用户交易信息 确认1
                user_trading_confirm_one_return(jsontext);
                break;
            case Agreement.user_trading_confirm_two_return://接收到用户交易信息 确认2
                user_trading_confirm_two_return(jsontext);
                break;
            case Agreement.user_trading_confirm_three_return://接收到用户交易信息 确认3
                user_trading_confirm_three_return(jsontext);
                break;
            case Agreement.user_trading_confirm_final_return://接收到用户交易信息 最后确认
                user_trading_confirm_final_return(jsontext);
                break;

            default: break;
        }
    }

     
    public virtual void user_offline_return(JsonData jsontext)
    { 
    }
    public virtual void user_chat_return(JsonData jsontext)
    {
    }
    public virtual void user_battle_return(JsonData jsontext)
    {
    }
    public virtual void user_information_return(JsonData jsontext)
    {
    }
    public virtual void user_trading_return(JsonData jsontext)
    {
    }
    public virtual void user_trading_cancel_return(JsonData jsontext)
    {
    }
    public virtual void user_trading_confirm_one_return(JsonData jsontext)
    {
    }
    public virtual void user_trading_confirm_two_return(JsonData jsontext)
    {
    }
    public virtual void user_trading_confirm_three_return(JsonData jsontext)
    {
    }
    public virtual void user_trading_confirm_final_return(JsonData jsontext)
    {
    } 
    void Update()
    {
        if (JsonList.Count > 0)
        {
            JsonData mes = JsonList.Dequeue();
            SplitDisposal(mes);
        }
    }

    //服务端返回后,数据赋值这里处理
    void SplitDisposal(JsonData mes)
    { 
        String Agreementstr = mes["Agreement"].ToString();
        switch (Agreementstr)
        {
            case Agreement.user_chat_return:

                print(mes["username"] + "=====" + mes["message"] + "---" + mes["for"]); 
                break;

            default: break;
        
        }
    }
   
    //根据需要 调用以下方法 
    public override void user_chat_return(JsonData jsontext)
    { 
        base.user_chat_return(jsontext);//特殊字符提前处理 
        print(jsontext["username"] + "=====" + jsontext["message"] + "---" + jsontext["for"]); 
        JsonList.Enqueue(jsontext);
        
    }
 
    public override void user_battle_return(JsonData jsontext)
    {
    }
    public override void user_information_return(JsonData jsontext)
    {
    }

 

posted on 2015-05-23 19:57  太粗难进  阅读(3502)  评论(0编辑  收藏  举报