我正在使用C#和WebSocket4Net库构建一个安全的WebSocket4Net客户机。我希望通过一个标准代理代理我所有的连接。
这个库使用SuperSocket.ClientEngine.Common.IProxyConnector来指定websocket连接的代理,但我不知道该如何实现它。
有没有人在这个图书馆工作过,并能提供一些建议?
发布于 2014-05-01 21:38:37
为了方便调试,我不得不做同样的事情,通过Fiddler来推动所有的websocket连接。因为WebSocket4Net作者选择重用他的IProxyConnector接口,所以System.Net.WebProxy不能直接使用.
在此链接上,作者建议使用他的父库SuperSocket.ClientEngine的实现,您可以从CodePlex下载这些实现,并包括SuperSocket.ClientEngine.Common.dll和SuperSocket.ClientEngine.Proxy.dll。--我不建议这样做。这会导致编译问题,因为他(很差)选择了在两个dll中定义的ClientEngine和WebSocket4Net都使用相同的名称空间。
什么对我有用:
为了让它通过Fiddler进行调试,我将这两个类复制到我的解决方案中,并将它们更改为本地命名空间:
HttpConnectProxy似乎在下面一行中有一个bug:
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!");
}https://stackoverflow.com/questions/23024121
复制相似问题