C# 如何在WebSocket4Net库中使用代理
我正在使用C#和WebSocket4Net库构建一个安全的WebSockets客户端。我希望我的所有连接都通过标准代理进行代理 这个库使用C# 如何在WebSocket4Net库中使用代理,c#,.net,websocket,websocket4net,C#,.net,Websocket,Websocket4net,我正在使用C#和WebSocket4Net库构建一个安全的WebSockets客户端。我希望我的所有连接都通过标准代理进行代理 这个库使用SuperSocket.ClientEngine.Common.IProxyConnector来指定websocket连接的代理,但我不确定应该如何实现它 有人使用过这个库并能提供一些建议吗?我也必须这样做,通过Fiddler推送所有websocket连接,以便于调试。由于WebSocket4Net作者选择重新使用其IProxyConnector接口,Syst
SuperSocket.ClientEngine.Common.IProxyConnector
来指定websocket连接的代理,但我不确定应该如何实现它
有人使用过这个库并能提供一些建议吗?我也必须这样做,通过Fiddler推送所有websocket连接,以便于调试。由于WebSocket4Net作者选择重新使用其
IProxyConnector
接口,System.Net.WebProxy
不能直接使用
关于这一点,作者建议使用父库SuperSocket.ClientEngine
中的实现,您可以从CodePlex下载该库,并包括SuperSocket.ClientEngine.Common.dll
和SuperSocket.ClientEngine.Proxy.dll
我不建议这样做。这会导致编译问题,因为他(很差)选择对ClientEngine
和WebSocket4Net
使用相同的命名空间,并在这两个dll中定义了IProxyConnector
什么对我有用: 为了通过Fiddler进行调试,我将这两个类复制到我的解决方案中,并将它们更改为本地名称空间:
如果(例如UserToken是DnsEndPoint)
改为:
如果(例如UserToken是DnsEndPoint | | targetEndPoint是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!");
}
私有WebSocket\u套接字;
公共初始化()
{
//初始化客户端连接
_socket=newwebsocket(“ws://echo.WebSocket.org”,来源:http://example.com");
//通过代理进行测试
var proxy=新的HttpConnectProxy(新的IPEndPoint(IPAddress.Parse(“127.0.0.1”),8888));
_socket.Proxy=(SuperSocket.ClientEngine.IProxyConnector)代理;
//钩住所有事件处理
_socket.Opened+=新事件处理程序(OnSocketOpened);
//_socket.Error+=新的事件处理程序(OnSocketError);
//_socket.Closed+=新的事件处理程序(OnSocketClosed);
//_socket.MessageReceived+=新事件处理程序(OnSocketMessageReceived);
//如果url已定义,请打开连接
如果(!String.IsNullOrWhiteSpace(url))
_socket.Open();
}
OnSocketOpened上的私有void(对象发送方,事件参数e)
{
//发送消息
_socket.Send(“你好,世界!”);
}
有没有办法使用此解决方案向代理进行身份验证?这里出现了许多关于代理的异常,一个接一个,先是数据太多,然后是协议不兼容,然后代理拒绝了连接(也就是说,它没有返回状态码2xx)。。。请更新答案?我在_socket处也遇到异常。在HttpConnectProxy.cs中的public override void Connect(EndPoint remoteEndPoint)处,引发有关remoteEndPoint的异常的Open()为null。