当前位置: 首页>>代码示例>>C#>>正文


C# WebSocketServer.GetAllSessions方法代码示例

本文整理汇总了C#中WebSocketServer.GetAllSessions方法的典型用法代码示例。如果您正苦于以下问题:C# WebSocketServer.GetAllSessions方法的具体用法?C# WebSocketServer.GetAllSessions怎么用?C# WebSocketServer.GetAllSessions使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在WebSocketServer的用法示例。


在下文中一共展示了WebSocketServer.GetAllSessions方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: socketServer_SendToAll

 static void socketServer_SendToAll(WebSocketServer socketServer, string message)
 {
     foreach (var socketSession in socketServer.GetAllSessions()) {
         socketServer_Send (socketSession, message);
     }
 }
开发者ID:kenlimmj,项目名称:quetzalcoatl,代码行数:6,代码来源:Program.cs

示例2: StartSuperWebSocketByProgramming

        static void StartSuperWebSocketByProgramming()
        {
            socketServer = new WebSocketServer();
            //设置websocket服务器
            if (!socketServer.Setup(20123)) //设置监听端口号
            {
                using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\log.txt", true))
                {
                    sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "设置WebSocketServer监听服务器失败");
                }
                return;
            }

            socketServer.NewMessageReceived += new SessionHandler<WebSocketSession, string>(socketServer_NewMessageReceived);
            socketServer.NewSessionConnected += new SessionHandler<WebSocketSession>(socketServer_NewSessionConnected);
            socketServer.SessionClosed += new SessionHandler<WebSocketSession, CloseReason>(socketServer_SessionClosed);

            //启动websocket服务器
            if (!socketServer.Start())
            {
                using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\log.txt", true))
                {
                    sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "启动服务器失败");
                }
                return;
            }
            using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\log.txt", true))
            {
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "启动服务器已成功启动2222222");
            }
            //var ts = new TimeSpan(0, 0, 8);

            //m_SocketPushTimer = new Timer(SendRealBusDataCallback, new object(), ts, ts);

            string str;

            while (true)
            {

                try
                {
                    qdRealBusDataClassesDataContext datacontext = new qdRealBusDataClassesDataContext();
                    var data = (from a in datacontext.RealBus
                                //where a.actdatetime > DateTime.Now.AddMinutes(-5)
                                group a by a.busselfid
                                    into b
                                    select new
                                    {
                                        id = b.Key,
                                        pl = (from c in b
                                              orderby c.actdatetime descending
                                              select new
                                              {
                                                  lng = c.lng,
                                                  lat = c.lat,
                                                  time = string.Format("{0:yyyy-MM-dd HH:mm:ss}", c.actdatetime),
                                                  linename = c.busLineName,
                                                  stop = c.BusStopsName,
                                                  num = c.stationnum,
                                                  name = c.stationname
                                              }).Take(1)
                                    });
                    JavaScriptSerializer serializer = new JavaScriptSerializer();
                    str = serializer.Serialize(data);
                    if (socketServer.GetAllSessions().Count() > 0  && str != null)
                    {
                        SendToAll(str);
                    }
                    Thread.Sleep(120000);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("异常错误:" + ex.Message);
                    using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\log.txt", true))
                    {
                        sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss 异常错误:") + ex.Message);
                    }
                }
                finally
                {

                }

            }
        }
开发者ID:yingtaoxiaowanzilixuan,项目名称:i4bus,代码行数:85,代码来源:qdRealBusService.cs

示例3: Window_Loaded


//.........这里部分代码省略.........
                string[] thId = json.CreateJSONPair("id", th.id.ToString());
                string[] thFromAddress = json.CreateJSONPair("fromAddress", json.CreateJSONArray(th.prevTxOuts.Select((elem) => new Account.AccountAddress(elem.Address).ToString()).Distinct().ToArray()));
                string[] thToAddress = json.CreateJSONPair("toAddress", json.CreateJSONArray(th.transaction.TxOutputs.Select((elem) => new Account.AccountAddress(elem.Address).ToString()).Distinct().ToArray()));
                string[] thAmount = json.CreateJSONPair("amount", th.amount.AmountInCreacoin.Amount);

                return json.CreateJSONObject(thValidity, thState, thBlockIndex, thType, thDatetime, thId, thFromAddress, thToAddress, thAmount);
            };

            Func<List<TransactionHistory>, string[]> _CreateTransactionHistoriesJSON = (ths) =>
            {
                List<string[]> transactionHistoriesList = new List<string[]>();
                foreach (var th in ths)
                    transactionHistoriesList.Add(_CreateTransactionHistoryJSON(th));
                return json.CreateJSONArray(transactionHistoriesList.ToArray());
            };

            Func<Program.LogData, string[]> _CreateLogJSON = (log) =>
            {
                string[] logType = json.CreateJSONPair("type", log.Kind.ToString());
                string[] logMessage = json.CreateJSONPair("message", log.ToString().Replace("\\", "\\\\").Replace("/", "\\/").Replace("\"", "\\\"").Replace(Environment.NewLine, "<br />").Replace("\n", "<br />").Replace("\r", "<br />"));
                return json.CreateJSONObject(logType, logMessage);
            };

            Func<Chat, string[]> _CreateChatJSON = (chat) =>
            {
                string[] chatName = json.CreateJSONPair("name", chat.NameWithTrip);
                string[] chatMessage = json.CreateJSONPair("message", chat.Message);
                return json.CreateJSONObject(chatName, chatMessage);
            };

            Action<string> _SendMessage = (message) =>
            {
                if (wss != null)
                    foreach (var wssession in wss.GetAllSessions())
                        wssession.Send(message);
            };

            Action<string[]> _SendMessages = (messages) =>
            {
                if (wss != null)
                    foreach (var wssession in wss.GetAllSessions())
                        foreach (var message in messages)
                            wssession.Send(message);
            };

            core.BalanceUpdated += (sender2, e2) => _SendMessage("balanceUpdated " + string.Join(Environment.NewLine, _CreateBalanceJSON()));
            core.blockChain.Updated += (sender2, e2) => _SendMessage("blockchainUpdated " + string.Join(Environment.NewLine, json.CreateJSONObject(json.CreateJSONPair("currentBlockIndex", core.blockChain.headBlockIndex))));
            core.transactionHistories.InvalidTransactionAdded += (sender2, e2) => _SendMessage("invalidTxAdded " + string.Join(Environment.NewLine, _CreateTransactionHistoryJSON(e2)));
            core.transactionHistories.InvalidTransactionRemoved += (sender2, e2) => _SendMessage("invalidTxRemoved " + string.Join(Environment.NewLine, json.CreateJSONObject(json.CreateJSONPair("id", e2.id.ToString()))));
            core.transactionHistories.UnconfirmedTransactionAdded += (sender2, e2) => _SendMessage("unconfirmedTxAdded " + string.Join(Environment.NewLine, _CreateTransactionHistoryJSON(e2)));
            core.transactionHistories.UnconfirmedTransactionRemoved += (sender2, e2) => _SendMessage("unconformedTxRemoved " + string.Join(Environment.NewLine, json.CreateJSONObject(json.CreateJSONPair("id", e2.id.ToString()))));
            core.transactionHistories.ConfirmedTransactionAdded += (sender2, e2) => _SendMessage("confirmedTxAdded " + string.Join(Environment.NewLine, _CreateTransactionHistoryJSON(e2)));
            core.transactionHistories.ConfirmedTransactionRemoved += (sender2, e2) => _SendMessage("confirmedTxRemoved " + string.Join(Environment.NewLine, json.CreateJSONObject(json.CreateJSONPair("id", e2.id.ToString()))));
            logger.LogAdded += (sender2, e2) => _SendMessage("logAdded " + string.Join(Environment.NewLine, _CreateLogJSON(e2)));
            core.iCreaNodeTest.ReceivedNewChat += (sender2, e2) => _SendMessage("chatAdded " + string.Join(Environment.NewLine, _CreateChatJSON(e2)));

            Action _SendAccountHolders = () => _SendMessages(new string[] { "aahUpdated " + string.Join(Environment.NewLine, _CreateAahJSON()), "pahsUpdated " + string.Join(Environment.NewLine, _CreatePahsJSON()) });

            EventHandler _AccountChanged = (sender2, e2) => _SendAccountHolders();
            EventHandler<IAccount> _AccountAdded = (sender2, e2) =>
            {
                _SendAccountHolders();

                e2.iAccountChanged += _AccountChanged;
            };
            EventHandler<IAccount> _AccountRemoved = (sender2, e2) =>
开发者ID:pizyumi,项目名称:CREA,代码行数:67,代码来源:MainWindow.xaml.cs


注:本文中的WebSocketServer.GetAllSessions方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。