.Net版Websocket简单封装

websocket 服务端使用的是Fleck编写

using Fleck;
using HomeMediaFileManageSystemUtilts.Log;
using System;
using System.Collections;
using System.Configuration;
namespace UploadAndDownloadTool.Helper.WebSocket
{

    /// 
    /// WebSocket帮助类是帮助后台向前台发送数据
    /// 而不需要前台主动请求的一个类
    /// 支持群发 单发
    /// 避免前台使用Ajax轮询   造成不必要的资源消耗
    /// 这个WebSocket  使用的是FLeck实现 类似的有SignalR(微软自家的)
    /// 
    public class WebSocketHelper
    {

        //保存客户端连接对象
        static Hashtable htSocket = new Hashtable();
        //WebSocket的实例
        static WebSocketServer socket = default(WebSocketServer);
        //会话Id
        static string SessionId = string.Empty;
        #region WebSocket服务器
        /// 
        /// WebSocket服务器(连接)
        /// 
        /// 
        /// 
        public static void WebSocketByServer(string url=default(string))
        {
            var SessoionId = string.Empty;
            //调试级别
            FleckLog.Level = LogLevel.Warn;
            try
            {
                if (string.IsNullOrEmpty(url))
                {
                    url = ConfigurationManager.AppSettings["SocketUrl"].ToString();
                }
                socket = new WebSocketServer(url);
                socket.Start(x =>
                {
                    x.OnOpen = () =>
                    {
                        SessoionId = x.ConnectionInfo.Id.ToString();
                    //添加到HashTable
                    htSocket.Add(SessoionId.ToString(), x);
                        Log.Warn("客户端连接ID: " + SessoionId.ToString());
                        Log.Warn("客户端地址:" + x.ConnectionInfo.ClientIpAddress);
                        Log.Warn("已连接");
                    //连接成功时把当前连接的ID发送到前台
                    x.Send(x.ConnectionInfo.Id.ToString());
                    };
                    x.OnClose = () =>
                    {
                    //刷新页面或关闭断开 把SessionId清空
                    SessionId = string.Empty;
                        htSocket.Remove(x.ConnectionInfo.Id);
                        Log.Debug("已关闭");
                    };
                });
            }
            catch (Exception ex)
            {
                Log.Warn("WebSocket连接失败;失败信息:" + ex.Message);
                Log.Warn("------------------------详细信息----------------------------");
                Log.Warn("WebSocket连接失败;失败信息:" + ex.StackTrace);
            }

        }
        #endregion

        #region 主动向页面发送数据(群发)
        /// 
        /// 后台向前台发送信息(群发)
        /// 
        /// 要发送的信息
        public static void AutoSendToPages(object sendMsg)
        {
            //遍历Keys 根据Keys  获取值转换WebSocketConnection对象
            foreach (string value in htSocket.Keys)
            {
                if (null != htSocket[value])
                {
                    var UserInfo = htSocket[value] as WebSocketConnection;
                    UserInfo.Send(sendMsg.ToString());
                }
            }
          

        }
        #endregion

        #region 主动向页面发送数据(单发)
        /// 
        /// 主动向页面发送数据(单发)
        /// 
        /// 
        /// 
        public static void AutoSendToPage(string socketId, object message)
        {
            if (null != htSocket[socketId])
            {
                var userSocket = htSocket[socketId] as WebSocketConnection;
                userSocket.Send(message.ToString());
            }
        }
        #endregion

    }
}

你可能感兴趣的:(Helper,C#,C#,.net)