C # Websocket instance

Source: Internet
Author: User

C # Websocket instance
This blog is a reference to the above example to teach you how to use websocket on the client and server. For those who are good at English, you can simply look at the source code above, the following explains how to use websocket: before we talk about it, let's take a look at which browsers support websocket: webSocket client supports browser support Chrome version 4 + supports Firefox version 5 + supports IE version 10 + supports Safari IOS 5 + supports Android Brower Android 4.5 + supports WebSocket server support vendor applications server remarks support for IBM WebSphere 8.0 and later, versions earlier than 7.X, combined with MQTT support for similar HTTP persistent connection to Oracle WebLogic 12c support, and 11g and 10g support similar HTTP Publish support HTTP persistent connection Microsoft IIS 7.0 + supports Apache Tomcat 7.0.5 + and 7.0.2X and 7.0.3X support Jetty 7.0 + through custom APIs. Now let's take a look at how the browser can achieve this: <! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">

using System;using System.Collections.Generic;using System.Linq;using System.Text;using Fleck;namespace TopoWebSocket{    class Program    {        static void Main(string[] args)        {            FleckLog.Level = LogLevel.Debug;            var allSockets = new List<IWebSocketConnection>();            var server = new WebSocketServer("ws://0.0.0.0:7181");            server.Start(socket =>            {                socket.OnOpen = () =>                {                    Console.WriteLine("Open!");                    allSockets.Add(socket);                };                socket.OnClose = () =>                {                    Console.WriteLine("Close!");                    allSockets.Remove(socket);                };                socket.OnMessage = message =>                {                    Console.WriteLine(message);                    allSockets.ToList().ForEach(s => s.Send("Echo: " + message));                };            });            var input = Console.ReadLine();            while (input != "exit")            {                foreach (var socket in allSockets.ToList())                {                    socket.Send(input);                }                input = Console.ReadLine();            }        }    }}

 

On the server side, I use the C # console program:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using Fleck;namespace TopoWebSocket{    class Program    {        static void Main(string[] args)        {            FleckLog.Level = LogLevel.Debug;            var allSockets = new List<IWebSocketConnection>();            var server = new WebSocketServer("ws://0.0.0.0:7181");            server.Start(socket =>            {                socket.OnOpen = () =>                {                    Console.WriteLine("Open!");                    allSockets.Add(socket);                };                socket.OnClose = () =>                {                    Console.WriteLine("Close!");                    allSockets.Remove(socket);                };                socket.OnMessage = message =>                {                    Console.WriteLine(message);                    allSockets.ToList().ForEach(s => s.Send("Echo: " + message));                };            });            var input = Console.ReadLine();            while (input != "exit")            {                foreach (var socket in allSockets.ToList())                {                    socket.Send(input);                }                input = Console.ReadLine();            }        }    }}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.