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


C# Socket.On方法代码示例

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


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

示例1: AttachSocketEvents

 private void AttachSocketEvents(Socket socket)
 {
     // Whenever the server emits "login", log the login message
     socket.On("login", data =>
     {
         var d = Data.FromData(data);
         connected = true;
         // Display the welcome message
         AddMessage("Welcome to Socket.IO Chat – ", true);
         AddParticipantsMessage(d.numUsers);
     });
     // Whenever the server emits "new message", update the chat body
     socket.On("new message", data =>
     {
         var d = Data.FromData(data);
         AddMessage(d.username, d.message);
     });
     // Whenever the server emits "user joined", log it in the chat body
     socket.On("user joined", data =>
     {
         var d = Data.FromData(data);
         AddMessage(d.username + " joined");
         AddParticipantsMessage(d.numUsers);
     });
     // Whenever the server emits "user left", log it in the chat body
     socket.On("user left", data =>
     {
         var d = Data.FromData(data);
         AddMessage(d.username + " left");
         AddParticipantsMessage(d.numUsers);
         UpdateChatTyping(d.username, true);
     });
     // Whenever the server emits "typing", show the typing message
     socket.On("typing", data =>
     {
         var d = Data.FromData(data);
         UpdateChatTyping(d.username, false);
     });
     // Whenever the server emits "stop typing", kill the typing message
     socket.On("stop typing", data =>
     {
         var d = Data.FromData(data);
         UpdateChatTyping(d.username, true);
     });
 }
开发者ID:mattleibow,项目名称:Socket.IO.Client,代码行数:45,代码来源:ChatFragment.cs

示例2: InitializeSocket

        public async Task InitializeSocket()
        {
            socketManager = new Core.Backend.Socket.IO("http://tugruacobackend.cloudapp.net:3000/");
            io = socketManager.GetSocket();

            io.On(Socket.EVENT_CONNECT, (data) =>
            {
                OnEvent(JToken.FromObject(new { Command = "EVENT_CONNECT", Data = "Conectado" }));
                SocketStatus = TuGrua.Core.Enums.SocketStatus.Connected;
            });

            io.On(Socket.EVENT_DISCONNECT, (data) =>
            {
                OnEvent(JToken.FromObject(new { Command = "EVENT_DISCONNECT", Data = "Desconectado" }));
                SocketStatus = TuGrua.Core.Enums.SocketStatus.Disconnected;
            });

            io.On(Socket.EVENT_RECONNECTING, (data) =>
            {
                OnEvent(JToken.FromObject(new { Command = "EVENT_RECONNECTING", Data = "Reconectando..." }));
                SocketStatus = TuGrua.Core.Enums.SocketStatus.Reconnecting;
            });

            io.On(Socket.EVENT_RECONNECT_FAILED, (data) =>
            {
                OnEvent(JToken.FromObject(new { Command = "EVENT_RECONNECT_FAILED", Data = "Reconexión fallida" }));
                SocketStatus = TuGrua.Core.Enums.SocketStatus.ReconnectFailed;
            });

            io.On(Socket.EVENT_CONNECT_ERROR, (data) =>
            {
                OnEvent(JToken.FromObject(new { Command = "EVENT_CONNECT_ERROR", Data = "Error en la conexión" }));
                SocketStatus = TuGrua.Core.Enums.SocketStatus.ConnectionError;
            });

            io.On(Socket.EVENT_CONNECT_TIMEOUT, (data) =>
            {
                OnEvent(JToken.FromObject(new { Command = "EVENT_CONNECT_TIMEOUT", Data = "El servidor no responde" }));
                SocketStatus = TuGrua.Core.Enums.SocketStatus.ConnectionTimeout;
            });

            io.On(Socket.EVENT_RECONNECT_ERROR, (data) =>
            {
                OnEvent(JToken.FromObject(new { Command = "EVENT_RECONNECT_ERROR", Data = "Error de reconexión" }));
            });

            io.On(Socket.EVENT_RECONNECT_ATTEMPT, (data) =>
            {
                OnEvent(JToken.FromObject(new { Command = "EVENT_RECONNECT_ATTEMPT", Data = "Intento de reconexión" }));
            });

            io.On("Welcome", (data) =>
            {
                OnEvent(JToken.FromObject(new { Command = "Welcome", Data = data }));
            });

            io.On("Backend.Message", (data) =>
            {
                OnEvent(JToken.FromObject(new { Command = "Backend.Message", Values = JObject.Parse(data.ToString()) }));
            });
        }
开发者ID:thEpisode,项目名称:TuGruaMobile,代码行数:61,代码来源:TuGrua.cs


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