我想使用websocket-sharp库在UWP应用中启动websocketserver。对于即兴点对点多人游戏,我需要这个。添加websocket-sharp.dll后,在构建应用程序时出现错误“在模块System.dll中找不到类型System.ComponentModel.Win32Exception”。
我尝试安装不同的NugetPackage,例如System.ComponentModel和NetStandart.Library
我无法显示特定的代码,因为该错误没有抛出该错误的行。 我使用了websocket sharp的这一部分。 MainPage.xaml.cs中的方法:
var httpsv = new HttpServer(4649);
httpsv.Log.Level = LogLevel.Trace;
httpsv.OnGet += (sender, e) =>
{
var req = e.Request;
var res = e.Response;
var path = req.RawUrl;
if (path == "/")
path += "index.html";
byte[] contents;
contents = Encoding.ASCII.GetBytes("Hallo");
if (path.EndsWith(".html"))
{
res.ContentType = "text/html";
res.ContentEncoding = Encoding.UTF8;
}
else if (path.EndsWith(".js"))
{
res.ContentType = "application/javascript";
res.ContentEncoding = Encoding.UTF8;
}
res.WriteContent(contents);
};
httpsv.AddWebSocketService<Chat>("/Chat");
httpsv.Start();
if (httpsv.IsListening)
{
Debug.WriteLine("Listening on port {0}, and providing WebSocket services:", httpsv.Port);
foreach (var path in httpsv.WebSocketServices.Paths)
Debug.WriteLine("- {0}", path);
}
Debug.WriteLine("\nPress Enter key to stop the server...");
while (true)
{
}
Chat.cs:
public class Chat : WebSocketBehavior
{
private string _name;
private static int _number = 0;
private string _prefix;
public Chat()
: this(null)
{
}
public Chat(string prefix)
{
_prefix = !prefix.IsNullOrEmpty() ? prefix : "anon#";
}
private string getName()
{
string name = "";
return !name.IsNullOrEmpty() ? name : _prefix + getNumber();
}
private static int getNumber()
{
return Interlocked.Increment(ref _number);
}
protected override void OnClose(CloseEventArgs e)
{
Sessions.Broadcast(String.Format("{0} got logged off...", _name));
}
protected override void OnMessage(MessageEventArgs e)
{
Sessions.Broadcast(String.Format("{0}: {1}", _name, e.Data));
}
protected override void OnOpen()
{
_name = getName();
}
}