Form1.cs 9.7 KB
Newer Older
W
wangzuohuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

/// 添加核心组件引用
using WrlEngine;
using WrlBase;

namespace PluginExeDemo
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// web socket服务对象
        /// </summary>
        SocketProxyClass WebSocketServer = null;
        /// <summary>
        /// web socket服务事件对象
        /// </summary>
        WebSocketEventSink WebSocketEvent = null;

        /// <summary>
        /// 命令行启动参数
        /// </summary>
        Dictionary<string, string> m_Para;

        public Form1(string strPara)
        {
            InitializeComponent();

            m_Para = new Dictionary<string, string>();

            /// 解析命令行参数
            string[] cmdArray = strPara.Split('&');
            foreach (string cmd in cmdArray)
            {
                string[] paraArray = cmd.Split('=');
                foreach (string para in paraArray)
                {
                    m_Para.Add(paraArray[0], paraArray[1]);
                    break;
                }
            }
        }

        public void Send(string strSID,string strContent)
        {
            WebSocketServer.AsynSendText(strSID,strContent);
        }

        public void OpenUrl(string strUrl)
        {
            this.IEBrowser.Navigate(strUrl);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            WebSocketServer = new SocketProxyClass();
            if (null == WebSocketServer)
                return;
            WebSocketEvent = new WebSocketEventSink();
            if (null == WebSocketEvent)
                return;
            WebSocketEvent.SetForm(this);
            ushort nPort = ushort.Parse(m_Para["PORT"]);
W
wangzuohuai 已提交
72
            /// 开始WebSocket侦听服务,返回实际侦听端口
W
wangzuohuai 已提交
73 74
            ushort nListenPort = WebSocketServer.Listen(nPort, m_Para["SID"], m_Para["AI"]);

W
wangzuohuai 已提交
75
            /// 建立事件通知
W
wangzuohuai 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
            WebSocketServer.NewConnEvent += WebSocketEvent.NewConnEvent;
            WebSocketServer.RecMsgEvent += WebSocketEvent.RecMsgEvent;
            WebSocketServer.RecTextEvent += WebSocketEvent.RecTextEvent;
            WebSocketServer.ConnCloseEvent += WebSocketEvent.ConnCloseEvent;
       }

        private void Form1_Closed(object sender, EventArgs e)
        {
            /// 移除事件通知
            WebSocketServer.NewConnEvent -= WebSocketEvent.NewConnEvent;
            WebSocketServer.RecMsgEvent -= WebSocketEvent.RecMsgEvent;
            WebSocketServer.RecTextEvent -= WebSocketEvent.RecTextEvent;
            WebSocketServer.ConnCloseEvent -= WebSocketEvent.ConnCloseEvent;
            /// 释放对象
            if (null != WebSocketServer)
            {
                WebSocketServer.Close();
                WebSocketServer = null;
            }

            WebSocketServer = null;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string strLastSID = WebSocketEvent.GetLastSID();
            if (null == strLastSID || 0 == strLastSID.Length)
            {
                MessageBox.Show("还未有来自网页的连接!");
                return;
            }
W
wangzuohuai 已提交
107
            Send(strLastSID,this.textBox1.Text);
W
wangzuohuai 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
            this.textBox1.Text = "";
         }

        /// <summary>
        /// WebSocket服务事件通知
        /// </summary>
        public class WebSocketEventSink : _ISocketProxyEvents
        {
            /// <summary>
            ///  主窗口
            /// </summary>

            Form1 m_Form;

            string m_strLastSID;

            public void SetForm(Form1 Form)
            {
                m_Form = Form;
            }

            public string GetLastSID()
            {
                return m_strLastSID;
            }

            public void WriteLog(string documentName, string msg)
            {
                string LogFilePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data");
                if (!System.IO.Directory.Exists(LogFilePath))
                    System.IO.Directory.CreateDirectory(LogFilePath);
                string logFile = System.IO.Path.Combine(LogFilePath, documentName + "@" + DateTime.Today.ToString("yyyy-MM-dd") + ".txt");
                bool writeBaseInfo = System.IO.File.Exists(logFile);
                StreamWriter swLogFile = new StreamWriter(logFile, true, Encoding.Unicode);
                swLogFile.WriteLine(DateTime.Now.ToString("HH:mm:ss") + "\t" + msg);
                swLogFile.Close();
                swLogFile.Dispose();
            }

            /// <summary>
W
wangzuohuai 已提交
148
            /// 通知收到新连接
W
wangzuohuai 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
            /// </summary>
            /// <param name="bstrSID"></param>
            public void NewConnEvent(string bstrSID)
            {
                m_strLastSID = bstrSID;
                m_Form.textBox2.AppendText("收到新连接:");
                m_Form.textBox2.AppendText(bstrSID);
                m_Form.textBox2.AppendText("\r\n");
            }

            /// <summary>
            /// 通知连接收到JSON数据包
            /// </summary>
            /// <param name="bstrSID"></param>
            /// <param name="nReqID"></param>
            /// <param name="bstrReqName"></param>
            /// <param name="bstrContent"></param>
            public void RecMsgEvent(string bstrSID, uint nReqID, string bstrPushName, string bstrMsg)
            {
W
wangzuohuai 已提交
168
                 m_Form.textBox2.AppendText("收到新数据包,请求序号:");
W
wangzuohuai 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
                m_Form.textBox2.AppendText(nReqID.ToString());
                m_Form.textBox2.AppendText("协议名:");
                m_Form.textBox2.AppendText(bstrPushName);
                m_Form.textBox2.AppendText("内容:");
                m_Form.textBox2.AppendText(bstrMsg);
                m_Form.textBox2.AppendText("\r\n");

                if (bstrPushName == "Demo_OpenUrl")
                {
                    /// 获得打开URL地址,调用浏览器打开
                    JsonServiceClass JsonService = new JsonServiceClass();
                    JsonService.ParseString(bstrMsg);
                    string strUrl = JsonService.GetStringValue("url");
                    JsonService = null;
                    m_Form.OpenUrl(strUrl);
                    return;
                }
                /// 回传给网页内容
                 m_Form.Send(bstrSID,"收到请求" + bstrPushName);
           }

            /// <summary>
            /// 通知连接收到文本内容
            /// </summary>
            /// <param name="bstrSID"></param>
            /// <param name="bstrText"></param>
            public void RecTextEvent(string bstrSID, string bstrText)
            {
                m_Form.textBox2.AppendText("收到文本内容:");
                m_Form.textBox2.AppendText(bstrText);
                m_Form.textBox2.AppendText("\r\n");
                /// 回传给网页内容
                m_Form.Send(bstrSID,"收到文本内容" + bstrText);
            }

            /// <summary>
W
wangzuohuai 已提交
205
            /// 通知连接收到字节流内容
W
wangzuohuai 已提交
206 207 208 209 210 211
            /// </summary>
            /// <param name="bstrSID"></param>
            /// <param name="Content"></param>
            /// <param name="nLen"></param>
            public void RecByteEvent(string bstrSID, Object Content, uint nLen)
            {
W
wangzuohuai 已提交
212 213 214 215 216 217 218 219
                m_Form.textBox2.AppendText("收到二进制流:");
                m_Form.textBox2.AppendText(Content.ToString());
                m_Form.textBox2.AppendText("\r\n");
            }

            /// <summary>
            /// 通知HTTP同步请求处理
            /// </summary>
W
wangzuohuai 已提交
220 221 222 223
            /// <param name="bstrSID"></param>
            /// <param name="bstrProtocol"></param>
            /// <param name="bstrUrl"></param>
            /// <param name="bstrPara"></param>
W
wangzuohuai 已提交
224 225 226 227 228 229
            /// <param name="pVal"></param>
            public void HttpReqEvent(string bstrSID, string bstrProtocol, string bstrUrl, string bstrPara, out string pVal)
            {
                /// 收到HTTP协议请求,主要用于前端同步请求,比如前端需要等待请求完成浏览器才能继续操作
                /// 这里执行阻塞操作,比如弹出模态对话框
                pVal = "{\"ret\":0,\"data\":{\"Ret\":0,\"Code\":1}}";
W
wangzuohuai 已提交
230 231 232
            }

            /// <summary>
W
wangzuohuai 已提交
233 234 235 236 237 238 239 240 241 242 243 244
            /// 通知HTTP侦听端口
            /// </summary>
            /// <param name="nPort"></param>
            public void HttpPortEvent(ushort nPort)
            {
                m_Form.textBox2.AppendText("HTTP侦听端口:");
                m_Form.textBox2.AppendText(nPort.ToString());
                m_Form.textBox2.AppendText("\r\n");
            }

            /// <summary>
            /// WS连接请求中出现错误
W
wangzuohuai 已提交
245
            /// </summary>
W
wangzuohuai 已提交
246 247 248
            /// <param name="bstrSID"></param>
            /// <param name="nReqID"></param>
            /// <param name="bstrErrInfo"></param>
W
wangzuohuai 已提交
249
            public void RecErrEvent(string bstrSID, uint nReqID, string bstrErrInfo)
W
wangzuohuai 已提交
250
            {
W
wangzuohuai 已提交
251 252 253
                m_Form.textBox2.AppendText("连接出现错误:");
                m_Form.textBox2.AppendText(bstrErrInfo);
                m_Form.textBox2.AppendText("\r\n");
W
wangzuohuai 已提交
254 255 256 257 258 259
            }

            /// <summary>
            /// 通知关闭连接
            /// </summary>
            /// <param name="bstrSID"></param>
W
wangzuohuai 已提交
260
            /// <param name="bstrReason"></param>
W
wangzuohuai 已提交
261
            public void ConnCloseEvent(string bstrSID, string bstrReason)
W
wangzuohuai 已提交
262
            {
W
wangzuohuai 已提交
263 264 265
                m_Form.textBox2.AppendText("关闭连接:");
                m_Form.textBox2.AppendText(bstrReason);
                m_Form.textBox2.AppendText("\r\n");
W
wangzuohuai 已提交
266 267 268 269
            }
        }
    }
}