首先需要下载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. CSS之float浮动

    CSS理解之float浮动 首先我们看看W3C给出的关于 float 的说明: 参考资料   MDN   W3C

  2. Spring学习_day02_AOP,AspectJ,JdbcTemplate

    本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! Spring_day02 一.AOP面向切面编程 1. ...

  3. codeforces_738C_二分

    C. Road to Cinema time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  4. Codeforces_731F_(前缀和)

    F. Video Cards time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  5. map集合遍历,放入id

    背景,需要从电脑导入excel表格到网页上然后表格中公司需要对应数据库的id 通过key-value方法来对应id Office office = new Office();office.setG00 ...

  6. 解决windows64位系统上安装mysql-python报错

    解决windows64位系统上安装mysql-python报错 2018年03月12日 13:08:24 一个CD包 阅读数:1231    版权声明:本文为博主原创文章,未经博主允许不得转载. ht ...

  7. Chromium CEF 2623 -- 支持 xp 的最后一个版本源码下载和编译步骤

    背景 因为项目需要在客户端中内嵌浏览器,需要支持 xp 操作系统和播放视频,但 CEF 2623 以后的版本已经不支持 xp 操作系统,也不再提供 2623 版本的二进制发布包下载,只好自己手动编译. ...

  8. lua_note_01_lua介绍

    1. lua 1. lua 1.1. lua介绍 1.2. Lua 特性 1.3. 特点 1.4. Lua 应用场景 1.5. 环境搭建 1.6. VS lua 1.1. lua介绍 Lua 是一种轻 ...

  9. 一篇入门AngularJS

    目录 1.AngularJS 应用 2.AngularJS 指令 3.AngularJS 表达式 4.AngularJS 模型 5.AngularJS 控制器 6.AngularJS 作用域 7.An ...

  10. Vue动态组件&异步组件

    在动态组件上使用keep-alive 我们之前曾经在一个多标签的界面中使用is特性来切换不同的组件: Vue.js的动态组件模板 <component v-bind:is="curre ...