NetClient.cs 3.97 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperSocket.ClientEngine;
using WebSocket4Net;

namespace NetLib
{
    public class NetClient
    {
        WebSocket ClientObj = null;

        public delegate void DelSessionConnected();
        public event DelSessionConnected SessionConnected;

        public delegate void DelSessionClosed();
        public event DelSessionClosed SessionClosed;

        public delegate void DelWebsocketError(Exception e);
        public event DelWebsocketError WebsocketError;

        public delegate void DelMessageReceived(string value);
        public event DelMessageReceived MessageReceived;

        public delegate void DelDataReceived(byte[] value);
        public event DelDataReceived DataReceived;

        public delegate void DelSessionErr(Exception ex);
        public event DelSessionErr SessionErr;

        public void Setup(string ipAddress, int port, List<KeyValuePair<string, string>> lstPar)
        {
            if (ClientObj != null)
            {
                Close();

                ClientObj.Opened -= Websocket_Opened;
                ClientObj.Error -= Websocket_Error;
                ClientObj.Closed -= Websocket_Closed;
                ClientObj.MessageReceived -= Websocket_MessageReceived;
                ClientObj.DataReceived -= Websocket_DataReceived; 
                ClientObj.Dispose();
            }
            
            ClientObj = new WebSocket("ws://" + ipAddress + ":" + port + "/", cookies: lstPar);

            ClientObj.Opened += Websocket_Opened;
            ClientObj.Error += Websocket_Error;
            ClientObj.Closed += Websocket_Closed;
            ClientObj.MessageReceived += Websocket_MessageReceived;
            ClientObj.DataReceived += Websocket_DataReceived;

        }

        private void Websocket_Opened(object sender, EventArgs e)
        {
            SessionConnected?.Invoke();
        }

        private void Websocket_Closed(object sender, EventArgs e)
        {
            SessionClosed?.Invoke();
        }

        private void Websocket_Error(object sender, ErrorEventArgs e)
        {
            string sErr = e.Exception + "";
            WebsocketError?.Invoke(e.Exception);
        }

        private void Websocket_MessageReceived(object sender, MessageReceivedEventArgs e)
        {
            MessageReceived?.Invoke(e.Message);
        }

        private void Websocket_DataReceived(object sender, DataReceivedEventArgs e)
        {
            DataReceived?.Invoke(e.Data);
        }

        public bool IsOpen
        {
            get
            {
                return ((ClientObj != null) && (ClientObj.State == WebSocketState.Open));
            }
        }

        public void Open()
        {
            if (ClientObj != null)
                ClientObj.Open();
        }
        public void Close()
        {
            try
            {
                if ((ClientObj != null) && (ClientObj.State != WebSocketState.Closed))
                {
                    ClientObj.Close();
                }
            }
            catch(Exception ex)
            {
                string sErr = ex + "";
            }
        }
        /// <summary>
        /// 杨斌 2020-05-17
        /// </summary>
        public void Send(string msg)
        {
            try
            {
                if (ClientObj != null)
                    ClientObj.Send(msg);
            }
            catch (Exception ex)
            {
                string sErr = "" + ex;
                SessionErr?.Invoke(ex);
            }            
        }
        /// <summary>
        /// 杨斌 2020-05-17
        /// </summary>
        public void Send(byte[] data)
        {
            try
            {
                if (ClientObj != null)
                    ClientObj.Send(data, 0, data.Length);
            }
            catch (Exception ex)
            {
                string sErr = "" + ex;
                SessionErr?.Invoke(ex);
            }            
        }
    }
}