首先須要下載DLL類庫 地址詳見:http://download.csdn.NET/detail/u011269801/9590935服務器
1,打開VS2012,新建一個控制檯應用程序,選擇.NET4.0版本session
2,添加引用 app
SuperSocket的dll文件(this
SuperSocket.Common.dll,spa
SuperSocket.SocketBase.dll,.net
SuperSocket.SocketEngine.dll)到此項目的引用 (版本選4.0)server
SuperWebSocket.dll 到此項目的引用get
添加 系統的 string
System.Configuration;it
System.Configuration.Install; 到此項目的引用
添加命名空間:
using SuperSocket.SocketBase;
using SuperWebSocket;
接下來請看實現
一、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;
}
}
}
二、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>";
}
}
}
三、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());
}
} }