【问题标题】:How to use proxies with the WebSocket4Net library如何在 WebSocket4Net 库中使用代理
【发布时间】:2014-05-26 06:57:05
【问题描述】:

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

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

有没有人使用过这个库并可以提供一些建议?

【问题讨论】:

    标签: c# .net websocket websocket4net


    【解决方案1】:

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

    this link 上,作者建议使用他的父库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!");
    }
    

    【讨论】:

    • 有没有办法通过这个解决方案通过代理进行身份验证?
    • 这里出现很多异常,一个接一个关于代理的异常,首先是数据太多,然后是不兼容的协议,然后代理拒绝连接(又名它没有返回状态码2xx)...请更新回答?
    • 我还在 _socket.Open() 处遇到异常,在 HttpConnectProxy.cs 中的 public override void Connect(EndPoint remoteEndPoint) 处抛出关于 remoteEndPoint 为 null 的异常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    相关资源
    最近更新 更多