首先需要下载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. easyui 使用笔记

    http://www.easyui.info/archives/1435.html datagrid 服务端分页 服务端分页,高效,快捷!强力推荐! easyui的datagrid服务端分页,通过设置 ...

  2. postfix 邮件中继配置

    Postfix 配置邮件中继 A 邮件发送服务器B 邮件中继服务器 A. 配置发件服务器 # 开启转发规则 [root@Postfix ~]# vi /etc/postfix/main.cf tran ...

  3. js 包管理工具

    环境 Windows10 + node 12.x + Webstorm 2019.1.3 工具 npm cnpm yarn npm/cnpm Webstorm 中第一次安装包一定几率卡死,很烦 不使用 ...

  4. sysbench基准测试工具

    一.简介SysBench是一个模块化的.跨平台.多线程基准测试工具,主要用于评估测试各种不同系统参数下的数据库负载情况.当前功能允许测试的系统参数有:file I/O performance (文件I ...

  5. solaris roles cannot login directly

    oracle@solaris:~$ su - root Password: Oracle Corporation SunOS root@solaris:~# cat /etc/user_attr # ...

  6. python爬虫20 | 小帅b教你如何使用python识别图片验证码

    当你在爬取某些网站的时候 对于你的一些频繁请求 对方会阻碍你 常见的方式就是使用验证码 验证码的主要功能 就是区分你是人还是鬼(机器人) 人 想法设法的搞一些手段来对付技术 而 技术又能对付人们的想法 ...

  7. PAT 1094. The Largest Generation (层级遍历)

    A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level bel ...

  8. Ubuntu | Flask + Gunicorn + Nginx 部署服务器环境

    现在我们手里有一个准备发布的项目,那么如何将他上传到你的服务器,并让外网访问呢? 前提: 1. 安装了Python环境 apt-get install python-dev 2. 安装Flask pi ...

  9. 用Twebbrowser做可控编辑器与MSHTML(调用js)

    记得intraweb的网页设计也程序开发分开中,是怎么定义的变量的.就是在网页中插入占位符.我们规定占位符是{%Name%} {%Birthday%}单页面装载之前or之后,我们用自己的js查找占位符 ...

  10. 【[Offer收割]编程练习赛12 D】 寻找最大值

    [题目链接]:http://hihocoder.com/problemset/problem/1496 [题意] [题解] 先把这n个数排个序吧. 这样相邻的数字就在一起了; 这样a[i]&a ...