首先需要下载DLL类库  地址详见:http://download.csdn.NET/detail/u011269801/9590935

1,打开VS2012,新建一个控制台应用程序,选择.NET4.0版本

2,添加引用

SuperSocket的dll文件(

SuperSocket.Common.dll,

SuperSocket.SocketBase.dll,

SuperSocket.SocketEngine.dll)到此项目的引用 (版本选4.0)

SuperWebSocket.dll   到此项目的引用

添加 系统的

System.Configuration;

System.Configuration.Install;  到此项目的引用

添加命名空间:

using SuperSocket.SocketBase;

using SuperWebSocket;

接下来请看实现

1、Player

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace WebSocketSeviceConsole
{
 
    public class Player
    {
        public string sessionID { get; set; }
 
        public string Name { get; set; }
        public float X { get; set; }
 
        public float Y { get; set; }
 
        public float Z { get; set; }
        public Player(string id)
        {
            this.sessionID = id;
            Name = sessionID.Substring(0, 6);
            X = -0.66666F;
            Y = 1.59666F;
            Z = 0;
        }
 
    }
}

2、MakeDataToString

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace WebSocketSeviceConsole
{
 
    public class MakeDataToString
    {
        public static string PlayerString(Player p)
        {
            return IDstr(p) + Namestr(p) + Xstr(p) + Ystr(p) + Zstr(p);
        }
 
        public static string IDstr(Player p)
        {
            return "<id>" + p.sessionID + "</id>";
        }
 
        public static string Namestr(Player p)
        {
            return "<name>" + p.Name + "</name>";
        }
 
        public static string Xstr(Player p)
        {
            return "<X>" + p.X + "</X>";
        }
 
        public static string Ystr(Player p)
        {
            return "<Y>" + p.Y + "</Y>";
        }
 
        public static string Zstr(Player p)
        {
            return "<Z>" + p.Z + "</Z>";
        }
    }
}
 
3、WebSocketSeviceConsole
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
using SuperSocket.SocketBase;
using SuperWebSocket;
 
namespace WebSocketSeviceConsole
{
    class Program
    {
        static int ClientNum = 0;
        static void Main(string[] args)
        {
            Dictionary<string, Player> PlayerList = new Dictionary<string, Player>();
            List<Player> Player__List = new List<Player>();
            Console.WriteLine("SuperWebSocket(0.8).Source服务器\n 按任意键start the WebSocketServer!");
            Console.ReadKey();
            Console.WriteLine();
            var appServer = new WebSocketServer();
            if (!appServer.Setup(2000))
            {
                Console.WriteLine("Failed to setup!");
                Console.ReadKey();
                return;
            }
            appServer.NewSessionConnected += new SessionHandler<WebSocketSession>(appServer_NewClientConnected);
            appServer.NewMessageReceived += new SessionHandler<WebSocketSession, string>(appServer_NewMessageReceived);
            appServer.SessionClosed += new SessionHandler<WebSocketSession, CloseReason>(appServer_SessionClosed);
            Console.WriteLine();
            if (!appServer.Start())
            {
                Console.WriteLine("Failed to start!");
                Console.ReadKey();
                return;
            }
            Console.WriteLine("服务器启动成功, 按 'q' 退出服务器!");
            while (Console.ReadKey().KeyChar != 'q')
            {
                Console.WriteLine();
                continue;
            }
            appServer.Stop();
            Console.WriteLine();
            Console.WriteLine("The server was stopped!");
            Console.ReadKey();
        }
 
        static void appServer_NewClientConnected(WebSocketSession session)
        {
            session.Send("第一次给客户端发信息,服务器端: ");
            Player ps = new Player(session.SessionID);
            session.Send(MakeDataToString.PlayerString(ps));
            Console.WriteLine("客户端 :端口" + session.RemoteEndPoint.Port + "连接到服务器了!");
            ClientNum += 1;
            foreach (var ses in session.AppServer.GetAllSessions())
            {
                ses.Send("xxx加入了连接!");
            }
        }
 
        static void appServer_NewMessageReceived(WebSocketSession session, string message)
        {
            session.Send("欢迎登陆本系统: ");
            Console.WriteLine("有客户端消息" + message);
            Console.WriteLine("客户端数目" + ClientNum.ToString());
            foreach (var ses in session.AppServer.GetAllSessions())
            {
                ses.Send("给所有客户端广播发送的消息广播电台");
            }
        }
 
        static void appServer_SessionClosed(WebSocketSession session, CloseReason closeRs)
        {
            session.Close();
            Console.WriteLine("客户端" + session.RemoteEndPoint.Port + "断开了连接!");
            ClientNum -= 1;
            Console.WriteLine("客户端数目" + ClientNum.ToString());
        }
 
    }
}
 
 

C# SuperWebSocket服务端学习(二)的更多相关文章

  1. Netty入门之客户端与服务端通信(二)

    Netty入门之客户端与服务端通信(二) 一.简介 在上一篇博文中笔者写了关于Netty入门级的Hello World程序.书接上回,本博文是关于客户端与服务端的通信,感觉也没什么好说的了,直接上代码 ...

  2. php服务端学习感想

    php是全世界web开发领域最受欢迎的语言,学习php的人一般都会些前端,懂些html/js/css等,php用得最多的是用于写业务逻辑.如果浮于表面,写过几个月php的人和写几年php的人可能差别不 ...

  3. C# SuperWebSocket服务端、客户端学习(三)

    1.打开VS2012,新建一个windows窗体程序,选择.NET4.0版本 2.添加引用 SuperSocket的dll文件( SuperSocket.Common.dll, SuperSocket ...

  4. Swift3.0服务端开发(二) 静态文件添加、路由配置以及表单提交

    今天博客中就来聊一下Perfect框架的静态文件的添加与访问,路由的配置以及表单的提交.虽然官网上有聊静态文件的访问的部分,但是在使用Perfect框架来访问静态文件时还是有些点需要注意的,这些关键点 ...

  5. 通过C#实现OPC-UA服务端(二)

    前言 通过我前面的一篇文件,我们已经能够搭建一个OPC-UA服务端了,并且也拥有了一些基础功能.这一次咱们就来了解一下OPC-UA的服务注册与发现,如果对服务注册与发现这个概念不理解的朋友,可以先百度 ...

  6. boost库在工作(32)网络服务端之二

    在这个例子里,服务器对象主要使用boost::asio::io_service对象,这个对象主要用来构造异步接收数据使用,接着定义boost::asio::ip::tcp::acceptor对象,这个 ...

  7. docker下编译mangoszero WOW60级服务端(二)

    开始搭建基于docker的mangoszero WOW服务端,我自己的操作系统是mac os,其他平台操作可以等价替换 1.准备工作 (1) 安装docker,参考docker官方文档,https:/ ...

  8. 写个OAuth2.0的请求端来测试自己的OAuth2.0服务端(二)

    在上一篇文章中,我们介绍了怎么创建自己的服务器,现在我们开始写个client端,来测试. 我们创建一个MVC项目,叫TestOAuthClient 1. 代码开始 1)第一步,我们创建一个MainCo ...

  9. webservice快速入门-使用wsimport生成ws服务端(二)

    上个例子演示的是在当前项目下发布的Webservice Server,而实际应用中和Client是分离的,本文介绍两种客户端开发方式: 1.导出WebService服务端服务接口到jar包,客户端引入 ...

随机推荐

  1. RTL Compiler之synthesis flow

    1 generic RTL Compiler work flow 2 invoking RTL compiler RTL Compiler is invoked from the operating ...

  2. seam的定时轮巡

    青岛的项目要做一个功能,每天凌晨2点的时候保存一次设备数据,这个就要求项目能够间隔24小时每天去做这个事,是一个自主轮巡. seam框架正好提供了这个功能,@Expiration指定开始时间,@Int ...

  3. GeckoWebBrowser设置cookie

    var uri = new Uri("http://www.aa.com"); //often cookies are stored on domain level, so &qu ...

  4. Mapreduce代码疑点(1)

    一.Hadoop MultipleInputs.addInputPath 读取多个路径 https://blog.csdn.net/t1dmzks/article/details/76473905 M ...

  5. Linux之FTP/TFTP(vsftp、vsftpd) HTTP(httpd、apache) DHCP(dhcpd)

    FTP/TFTP(vsftp.vsftpd): FTP是File Transfer Protocol(文件传输协议)而中文简称为"文传协议".用于Internet上的控制文件的双向 ...

  6. css3 background-clip和background-origin 区别

    在css3中,background-clip和background-origin它们2个的功能大致相同,但又有些细微差别. 1.background-clip:规定背景的绘制区域,当背景是纯颜色时与图 ...

  7. 《Java JDK 8 学习笔记》序

    摘录自<Java JDK 8 学习笔记> 翻开一本书,无非是想从书中得到知识,只是为何你要得到书中的知识,才是我想知道的答案,而这个答案决定了你在取得知识的过程中是否快乐! 多数人在取得知 ...

  8. 【Mail.Ru Cup 2018 Round 2 A】 Metro

    [链接] 我是链接,点我呀:) [题意] [题解] 1:一直往右走的情况. 2:中间某个地方中转 (不会出现超过1次的转弯. (如果超过了和1次是等价的 [代码] #include <bits/ ...

  9. Linux imooc learning

    https://www.imooc.com/video/3529 Windows Vs Linux Linux: (other linux overall   https://onedrive.liv ...

  10. R语言 PCA

    1.关键点 综述:主成分分析 因子分析 典型相关分析,三种方法的共同点主要是用来对数据降维处理的从数据中提取某些公共部分,然后对这些公共部分进行分析和处理. #主成分分析 是将多指标化为少数几个综合指 ...