您的位置:首页 > 编程语言 > C#

C# SuperWebSocket服务端学习(二)

2017-08-03 16:58 363 查看
首先需要下载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());
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: