websocket与socket学习,互相打通【目前来说最全的实战案例】

压力测试

先来个示例效果图

原理图

 


             

try
             {
                 wsSIP = ConfigurationManager.AppSettings["APWebSocketIP"]; //地址
                 wsSPort =int.Parse(ConfigurationManager.AppSettings["APWebSocketPort"]);
                 websocket = new WebSocket("ws://" + wsSIP + ":" + wsSPort);//注意地址
                 websocket.Opened += new EventHandler(websocket_Opened);
                 websocket.Error += new EventHandler<ErrorEventArgs>(websocket_Error);
                 websocket.Closed += new EventHandler(websocket_Closed);
                 websocket.MessageReceived += websocket_MessageReceived;
                 websocket.Open();
                 //发送时websocket.send("winform:内容");
                 websocket_conn = true;
                 btn_wsfc_conn.Enabled = !websocket_conn;//连接按钮
             }
             catch
             {
                 websocket_conn = false;
                 btn_wsfc_conn.Enabled = true;
             } 

      websocket的Form客户端再load里加入代码

        private void websocket_MessageReceived(object sender, MessageReceivedEventArgs e)
        {
            MessageReceivedEventArgs responseMsg = (MessageReceivedEventArgs)e; //接收服务端发来的消息
            string strMsg = responseMsg.Message;
            wsFormClientTxt(strMsg);//显示数据

        }
        private void websocket_Closed(object sender, EventArgs e)
        {
            wsFormClientTxt("websocket_Closed断开连接" + websocket.LocalEndPoint);
        }

        void websocket_Opened(object sender, EventArgs e)
        {
            wsFormClientTxt("websocket_Opened连接ws服务器成功" + websocket.LocalEndPoint);
        }
        void websocket_Error(object sender, ErrorEventArgs e)
        {
            wsFormClientTxt("websocket_Error|" + e.Exception.Message.ToString());
        }

       //设置回显,时间,十六进制,回车换行,自动清空

        void wsFormClientTxt(string txt)
        {
            if (this.checkBox12.Checked)
                txt = string.Format("{0:HH:mm:ss fff}", DateTime.Now.ToLongTimeString()) + " " + txt;
            //if (this.checkBox13.Checked)
            //    txt = Str2Hex(txt).ToUpper();//16进制大写
            if (this.checkBox11.Checked)
                txt += "\r\n";//回车换行            
            txt_wsCRecv.AppendText(txt);

            if (txt_wsCRecv.Text.Length > 1000)
                txt_wsCRecv.Text = "";
        }

见(二)续...

新版客户端,带MQTT客户端,物联网案例体验(手机端推送同步,开灯关灯指示可自动切换)

温湿度暂未解析,已经实时推送(每5秒钟)!!!

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值