如何使用WebSocket4Net库代理

bli*_*izz 8 .net c# websocket websocket4net

我正在使用C#和WebSocket4Net库构建一个安全的WebSockets客户端.我希望我的所有连接都通过标准代理进行代理.

这个lib使用SuperSocket.ClientEngine.Common.IProxyConnector指定websocket连接的代理,但我不确定我应该如何实现它.

有没有人在这个图书馆工作,可以提供一些建议吗?

ars*_*in3 16

我必须这样做,通过Fiddler推送所有websocket连接,以便于调试.因为WebSocket4Net作者选择重用他的IProxyConnector界面,System.Net.WebProxy不能直接使用.

此链接上,作者建议使用其父库中的实现SuperSocket.ClientEngine,您可以从CodePlex下载并包含SuperSocket.ClientEngine.Common.dllSuperSocket.ClientEngine.Proxy.dll.我不推荐这个.这导致编译问题,因为他(很差)选择使用相同的命名空间ClientEngineWebSocket4Net两个dll中定义的IProxyConnector.


什么对我有用:

为了让它通过Fiddler进行调试,我将这两个类复制到我的解决方案中,并将它们更改为本地命名空间:

HttpConnectProxy似乎在以下行中有一个错误:

if (e.UserToken is DnsEndPoint)

改成:

if (e.UserToken is DnsEndPoint || targetEndPoint is DnsEndPoint)


在那之后,事情很好.示例代码:

private WebSocket _socket;

public Initialize()
{
    // initialize the client connection
    _socket = new WebSocket("ws://echo.websocket.org", origin: "http://example.com");

    // go through proxy for testing
    var proxy = new HttpConnectProxy(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888));
    _socket.Proxy = (SuperSocket.ClientEngine.IProxyConnector)proxy;

    // hook in all the event handling
    _socket.Opened += new EventHandler(OnSocketOpened);
    //_socket.Error += new EventHandler<ErrorEventArgs>(OnSocketError);
    //_socket.Closed += new EventHandler(OnSocketClosed);
    //_socket.MessageReceived += new EventHandler<MessageReceivedEventArgs>(OnSocketMessageReceived);

    // open the connection if the url is defined
    if (!String.IsNullOrWhiteSpace(url))
        _socket.Open();
}

private void OnSocketOpened(object sender, EventArgs e)
{
    // send the message
    _socket.Send("Hello World!");
}
Run Code Online (Sandbox Code Playgroud)

  • 有没有办法通过此解决方案与代理进行身份验证? (2认同)