在我搜索WebSockets庫時,我遇到了this website,它提供了Delphi和C#版本的下載部分。它吸引了我的注意力,特別是因爲我的應用程序的客戶端是使用Delphi開發的,我試圖用C#開發服務器端。使用靜態包裝類
查看C#聊天示例,我意識到它在Delphi中編寫的非託管DLL周圍使用了一個包裝類(sgcWebSocketLib)。下面是從sgcWebSocketLib.cs摘錄:
public sealed class sgcWebSocketLib
{
private static volatile sgcWebSocketLib instance;
private static object syncRoot = new Object();
private sgcWebSocketLib()
{
}
public static sgcWebSocketLib Instance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
instance = new sgcWebSocketLib();
}
}
return instance;
}
}
... //the rest is omitted
}
,並從開始按鈕,聊天服務器的代碼(典型的WinForms應用程序):
private void btnStart_Click(object sender, EventArgs e)
{
string vOptions = "";
... //setting options according to UI values
sgcWebSocketLib.Instance.Server_LoadOptions(vOptions);
sgcWebSocketLib.Instance.Server_Start();
}
現在,這裏是真正的問題是:這聊天服務器使用sgcWebSocketLib類的static
屬性,並開始發送/接收WebSocket的東西。我可以在ASP.Net應用程序(WebForms或MVC)中使用相同的方法嗎?我可以使用這個包裝類在ASP.Net中編寫聊天服務器嗎? PS:我知道有SignalR,也許還有其他的。但它具有一些限制(IIS 8,Windows Server 2012對WebSocket的需求),這與Delphi VCL客戶端的未答覆通信問題相同。
感謝您的詳細解答。所以,如果我想使用你的庫,我應該使用一個單獨的端口呢? – 2015-02-10 09:53:25
對。對於任何不是IIS8內置的WebSocket服務器,您需要提供不同的端口。 – vtortola 2015-02-10 13:51:58