首先需要下载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. 第二个Activity返回数据

    背景内容:FirstActivity先跳转到TwoActivity,再由TwoActivity返回,并还返回数据. 一般情况下Activity间跳转只需要调用 startActivity(Intent ...

  2. JS高级——沙箱

    基本概念 1.沙箱:与外界隔绝的一个环境,外界无法修改该环境内任何信息,沙箱内的东西单独属于一个世界 2.苹果手的app使用的就是沙箱模式去运行,隔离app的空间,每个app独立运行 js沙箱基本模式 ...

  3. Centos 安装 Moosefs文件系统

    一.环境介绍Moosefs master:192.168.55.148Moosefs Metalogger:192.168.55.149Moosefs Chunk-01:192.168.55.150M ...

  4. windons共享的一些问题

    有时候访问共享一直说无法打开共享,但是别人确实是开了共享. 其中可能如下: 1.首先确定网络没有问题,win+R输入cmd,ping对方IP地址,保证是网络是通的,如果不通,关闭共享电脑的防火墙. 2 ...

  5. 【源码阅读】opencv中opencl版本的dft函数的实现细节

    1.函数声明 opencv-3.4.3\modules\core\include\opencv2\core.hpp:2157 CV_EXPORTS_W void dft(InputArray src, ...

  6. Python os模块和time模块 day4

    一.os模块 print(os.listdir(r'/Users/smh/Desktop/整理'))#os.listdir() 列出某个目录下面的文件夹/文件 print(os.path.isfile ...

  7. 121. Best Time to Buy and Sell Stock(动态规划)

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  8. windows 实现vue命令行

    在代码编辑器里写好文件的位置,以及相关的命令,保存文件类型是.cmd

  9. Android第三方开源图片裁剪截取:cropper

     Android第三方开源图片裁剪截取:cropper 很多app都需要裁剪截取图片作为头像.logo之类,而cropper是github上的一个针对Android平台的.第三方开源图片裁剪截取项 ...

  10. 创建Spark执行环境SparkEnv

    SparkDriver 用于提交用户的应用程序, 一.SparkConf 负责SparkContext的配置参数加载, 主要通过ConcurrentHashMap来维护各种`spark.*`的配置属性 ...