Form1.cs 7.8 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 72 73 74 75 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 107 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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
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 ZbaEngine;
using ZbaBase;

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"]);
            ushort nListenPort = WebSocketServer.Listen(nPort, m_Para["SID"], m_Para["AI"]);

                /// 建立事件通知
            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;
            }
            Send(strLastSID, this.textBox1.Text);
            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>
            /// 通知新连接
            /// </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)
            {
                m_Form.textBox2.AppendText("收到新数据包,请求序号:");
                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>
            /// 
            /// </summary>
            /// <param name="bstrSID"></param>
            /// <param name="Content"></param>
            /// <param name="nLen"></param>
            public void RecByteEvent(string bstrSID, Object Content, uint nLen)
            {
            }

            /// <summary>
            /// 
            /// </summary>
            /// <param name="nSessionID"></param>
            /// <param name="strUrl"></param>
            /// <param name="strPara"></param>
            public void HttpReqEvent(uint nSessionID, string strUrl, string strPara)
            {
            }

            /// <summary>
            /// 通知关闭连接
            /// </summary>
            /// <param name="bstrSID"></param>
            public void ConnCloseEvent(string bstrSID)
            {
            }
        }
    }
}