2014-04-11 32 views
5

我正在使用C#和WebSocket4Net库构建安全的WebSockets客户端。我希望我的所有连接都可以通过标准代理进行代理。如何在WebSocket4Net库中使用代理服务器

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

有没有人与这个图书馆合作,并可以提供一些建议?

回答

12

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

this link笔者建议使用其父母库SuperSocket.ClientEngine的实现,您可以从CodePlex上下载并同时包括SuperSocket.ClientEngine.Common.dllSuperSocket.ClientEngine.Proxy.dll我不推荐这个。这会导致编译问题,因为他(很差)选择使用相同的命名空间与ClientEngineWebSocket4Net与在这两个DLL中定义的IProxyConnector。


什么工作对我来说:

为了得到它通过提琴手工作进行调试,我复制这两个类为我的解决方案,并将其改为本地命名空间:

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!"); 
} 
+0

这真的为我节省了一些时间 - 谢谢! – fcrick

+2

有没有办法使用此解决方案与代理进行身份验证? –

+0

在这里获得很多例外,一个接一个的关于代理,第一太多的数据,然后不兼容的协议,然后代理拒绝连接(又没有返回状态代码2xx)...请更新答案? – Gizmo

相关问题