本文整理汇总了C#中WebSocket4Net.WebSocket.Close方法的典型用法代码示例。如果您正苦于以下问题:C# WebSocket.Close方法的具体用法?C# WebSocket.Close怎么用?C# WebSocket.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WebSocket4Net.WebSocket
的用法示例。
在下文中一共展示了WebSocket.Close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReserveConnection
public static WebSocketDisposable ReserveConnection(string uri)
{
Tuple<WebSocket, RefCountDisposable> connection;
lock(openConnectionsLock)
{
if(!openConnections.TryGetValue(uri, out connection))
{
Debug.WriteLine("Creating new websocket");
WebSocket ws = new WebSocket(uri);
// Set callbacks?
ws.Open();
var dispose = Disposable.Create(() =>
{
Debug.WriteLine("Disposing of websocket");
ws.Close();
openConnections.Remove(uri);
});
RefCountDisposable refCount = new RefCountDisposable(dispose);
connection = Tuple.Create(ws, refCount);
openConnections.Add(uri, connection);
return new WebSocketDisposable(connection.Item1, connection.Item2.GetDisposable());
}
}
Debug.WriteLine("Returning old websocket");
return new WebSocketDisposable(connection.Item1, connection.Item2.GetDisposable());
}
示例2: connectSocket
private void connectSocket()
{
socket = new WebSocket(socketAdress, "", WebSocketVersion.Rfc6455);
if (socket.State == WebSocketState.Open)
socket.Close();
socket.EnableAutoSendPing = true;
socket.Closed += new EventHandler(socket_Closed);
socket.Error += new EventHandler<ErrorEventArgs>(socket_Error);
socket.MessageReceived += new EventHandler<MessageReceivedEventArgs>(socket_MessageReceived);
socket.Opened += socket_Opened;
socket.Open();
Debug.WriteLine("Cybergame socket connected.");
}
示例3: Main
static void Main(string[] args)
{
_webSocket = new WebSocket("ws://localhost:44961/wsapp?token=ABCD");
_webSocket.Opened += WebSocketOnOpened;
_webSocket.Closed += (sender, eventArgs) => Console.WriteLine("Closed");
_webSocket.Error += (sender, eventArgs) => Console.WriteLine("Error" + eventArgs.Exception);
_webSocket.MessageReceived += (sender, eventArgs) => Console.WriteLine(eventArgs.Message);
_webSocket.DataReceived += (sender, eventArgs) => Console.WriteLine("Data received");
_webSocket.Open();
while (Console.ReadKey().KeyChar != 'q')
{
Console.WriteLine();
}
_webSocket.Close();
}
示例4: TestConnection
public void TestConnection()
{
var webSocketClient = new WebSocket("ws://echo.websocket.org");
//webSocketClient.AllowUnstrustedCertificate = true;
webSocketClient.Opened += new EventHandler(webSocketClient_Opened);
webSocketClient.Closed += new EventHandler(webSocketClient_Closed);
webSocketClient.MessageReceived += new EventHandler<MessageReceivedEventArgs>(webSocketClient_MessageReceived);
webSocketClient.Open();
if (!m_OpenedEvent.WaitOne(5000))
{
Assert.Fail("Failed to Opened session ontime");
}
Assert.AreEqual(WebSocketState.Open, webSocketClient.State);
for (var i = 0; i < 10; i++)
{
var message = Guid.NewGuid().ToString();
webSocketClient.Send(message);
if (!m_MessageReceiveEvent.WaitOne(5000))
{
Assert.Fail("Failed to get echo messsage on time");
break;
}
Assert.AreEqual(m_CurrentMessage, message);
}
webSocketClient.Close();
if (!m_CloseEvent.WaitOne(5000))
{
Assert.Fail("Failed to close session ontime");
}
Assert.AreEqual(WebSocketState.Closed, webSocketClient.State);
}
示例5: FindAll
public List<SniperInfo> FindAll()
{
List<SniperInfo> newSniperInfos = new List<SniperInfo>();
try
{
using (var client = new WebSocket(URL, "basic", WebSocketVersion.Rfc6455))
{
client.MessageReceived += (s, e) =>
{
var message = e.Message;
var match = Regex.Match(message, @"^(1?\d+)\[""[a|b]"",""(2?.*)""\]$");
if (match.Success)
{
if (match.Groups[1].Value == "42")
{
var sniperInfos = Parse(match.Groups[2].Value);
if (sniperInfos != null && sniperInfos.Any())
{
newSniperInfos.AddRange(sniperInfos);
}
}
}
};
client.Open();
Thread.Sleep(Timeout);
client.Close();
}
}
catch (Exception e)
{
Log.Warn("Received error from Pokezz. More info the logs");
Log.Debug("Received error from Pokezz: ", e);
}
return newSniperInfos;
}
示例6: ConcurrentSendTest
public void ConcurrentSendTest()
{
WebSocket webSocketClient = new WebSocket(string.Format("{0}:{1}/websocket", Host, m_WebSocketServer.Config.Port), "basic", m_Version);
webSocketClient.AllowUnstrustedCertificate = true;
webSocketClient.Opened += new EventHandler(webSocketClient_Opened);
webSocketClient.Closed += new EventHandler(webSocketClient_Closed);
webSocketClient.Open();
if (!m_OpenedEvent.WaitOne(2000))
Assert.Fail("Failed to Opened session ontime");
string[] lines = new string[100];
for (int i = 0; i < lines.Length; i++)
{
lines[i] = Guid.NewGuid().ToString();
}
var messDict = lines.ToDictionary(l => l);
webSocketClient.MessageReceived += (s, m) =>
{
messDict.Remove(m.Message);
Console.WriteLine("R: {0}", m.Message);
};
Parallel.For(0, lines.Length, (i) =>
{
webSocketClient.Send("ECHO " + lines[i]);
});
int waitRound = 0;
while (waitRound < 10)
{
if (messDict.Count <= 0)
break;
Thread.Sleep(500);
waitRound++;
}
if (messDict.Count > 0)
{
Assert.Fail("Failed to receive message on time.");
}
webSocketClient.Close();
if (!m_CloseEvent.WaitOne(1000))
Assert.Fail("Failed to close session ontime");
}
示例7: SendDataTest
public void SendDataTest()
{
WebSocket webSocketClient = new WebSocket(string.Format("{0}:{1}/websocket", Host, m_WebSocketServer.Config.Port), "basic", m_Version);
if (!webSocketClient.SupportBinary)
return;
webSocketClient.AllowUnstrustedCertificate = true;
webSocketClient.Opened += new EventHandler(webSocketClient_Opened);
webSocketClient.Closed += new EventHandler(webSocketClient_Closed);
webSocketClient.DataReceived += new EventHandler<DataReceivedEventArgs>(webSocketClient_DataReceived);
webSocketClient.Open();
if (!m_OpenedEvent.WaitOne(2000))
Assert.Fail("Failed to Opened session ontime");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10; i++)
{
sb.Append(Guid.NewGuid().ToString());
}
string messageSource = sb.ToString();
Random rd = new Random();
for (int i = 0; i < 100; i++)
{
int startPos = rd.Next(0, messageSource.Length - 2);
int endPos = rd.Next(startPos + 1, messageSource.Length - 1);
string message = messageSource.Substring(startPos, endPos - startPos);
Console.WriteLine("Client:" + message);
var data = Encoding.UTF8.GetBytes(message);
webSocketClient.Send(data, 0, data.Length);
if (!m_MessageReceiveEvent.WaitOne(1000))
Assert.Fail("Cannot get response in time!");
Assert.AreEqual(message, m_CurrentMessage);
}
webSocketClient.Close();
if (!m_CloseEvent.WaitOne(1000))
Assert.Fail("Failed to close session ontime");
}
示例8: SendPingReactTest
public void SendPingReactTest()
{
WebSocket webSocketClient = new WebSocket(string.Format("{0}:{1}/websocket", Host, m_WebSocketServer.Config.Port), "basic", m_Version);
webSocketClient.AllowUnstrustedCertificate = true;
webSocketClient.Opened += new EventHandler(webSocketClient_Opened);
webSocketClient.Closed += new EventHandler(webSocketClient_Closed);
webSocketClient.MessageReceived += new EventHandler<MessageReceivedEventArgs>(webSocketClient_MessageReceived);
webSocketClient.Open();
if (!m_OpenedEvent.WaitOne(2000))
Assert.Fail("Failed to Opened session ontime");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10; i++)
{
sb.Append(Guid.NewGuid().ToString());
}
string messageSource = sb.ToString();
Random rd = new Random();
for (int i = 0; i < 10; i++)
{
int startPos = rd.Next(0, messageSource.Length - 2);
int endPos = rd.Next(startPos + 1, messageSource.Length - 1);
string message = messageSource.Substring(startPos, endPos - startPos);
Console.WriteLine("PING:" + message);
webSocketClient.Send("PING " + message);
}
Thread.Sleep(5000);
webSocketClient.Close();
if (!m_CloseEvent.WaitOne(1000))
Assert.Fail("Failed to close session ontime");
}
示例9: ReconnectTest
public void ReconnectTest()
{
WebSocket webSocketClient = new WebSocket(string.Format("{0}:{1}/websocket", Host, m_WebSocketServer.Config.Port), "basic", m_Version);
webSocketClient.Error += new EventHandler<SuperSocket.ClientEngine.ErrorEventArgs>(webSocketClient_Error);
webSocketClient.AllowUnstrustedCertificate = true;
webSocketClient.Opened += new EventHandler(webSocketClient_Opened);
webSocketClient.Closed += new EventHandler(webSocketClient_Closed);
webSocketClient.MessageReceived += new EventHandler<MessageReceivedEventArgs>(webSocketClient_MessageReceived);
for (var i = 0; i <2000; i++)
{
webSocketClient.Open();
if (!m_OpenedEvent.WaitOne(5000))
Assert.Fail("Failed to Opened session ontime at round {0}", i);
Assert.AreEqual(WebSocketState.Open, webSocketClient.State);
webSocketClient.Close();
if (!m_CloseEvent.WaitOne(5000))
Assert.Fail("Failed to close session ontime");
Assert.AreEqual(WebSocketState.Closed, webSocketClient.State);
}
}
示例10: UnreachableReconnectTestB
public void UnreachableReconnectTestB()
{
StopServer();
WebSocket webSocketClient = new WebSocket(string.Format("{0}:{1}/websocket", Host, m_WebSocketServer.Config.Port), "basic", m_Version);
webSocketClient.Error += new EventHandler<SuperSocket.ClientEngine.ErrorEventArgs>(webSocketClient_Error);
webSocketClient.Error += (s, e) => { m_OpenedEvent.Set(); };
webSocketClient.AllowUnstrustedCertificate = true;
webSocketClient.Opened += new EventHandler(webSocketClient_Opened);
webSocketClient.Closed += new EventHandler(webSocketClient_Closed);
webSocketClient.MessageReceived += new EventHandler<MessageReceivedEventArgs>(webSocketClient_MessageReceived);
webSocketClient.Open();
m_OpenedEvent.WaitOne();
Assert.AreEqual(WebSocketState.Closed, webSocketClient.State);
StartServer();
webSocketClient.Open();
if (!m_OpenedEvent.WaitOne(5000))
Assert.Fail("Failed to Opened session ontime");
Assert.AreEqual(WebSocketState.Open, webSocketClient.State);
m_CloseEvent.Reset();
webSocketClient.Close();
if (!m_CloseEvent.WaitOne(2000))
Assert.Fail("Failed to close session ontime");
Console.WriteLine("State {0}: {1}", webSocketClient.GetHashCode(), webSocketClient.State);
Assert.AreEqual(WebSocketState.Closed, webSocketClient.State);
}
示例11: SendMessageTest
public void SendMessageTest()
{
WebSocket webSocketClient = new WebSocket(string.Format("ws://127.0.0.1:{0}/websocket", m_WebSocketServer.Config.Port), "basic", m_Version);
webSocketClient.Opened += new EventHandler(webSocketClient_Opened);
webSocketClient.Closed += new EventHandler(webSocketClient_Closed);
webSocketClient.MessageReceived += new EventHandler<MessageReceivedEventArgs>(webSocketClient_MessageReceived);
webSocketClient.Open();
if (!m_OpenedEvent.WaitOne(1000))
Assert.Fail("Failed to Opened session ontime");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10; i++)
{
sb.Append(Guid.NewGuid().ToString());
}
string messageSource = sb.ToString();
Random rd = new Random();
for (int i = 0; i < 100; i++)
{
int startPos = rd.Next(0, messageSource.Length - 2);
int endPos = rd.Next(startPos + 1, messageSource.Length - 1);
string message = messageSource.Substring(startPos, endPos - startPos);
webSocketClient.Send("ECHO " + message);
Console.WriteLine("Client:" + message);
if (!m_MessageReceiveEvent.WaitOne(1000))
Assert.Fail("Cannot get response in time!");
Assert.AreEqual(message, m_CurrentMessage);
}
webSocketClient.Close();
if (!m_CloseEvent.WaitOne(1000))
Assert.Fail("Failed to close session ontime");
}
示例12: Register
public override bool Register(string Username, string Password)
{
try
{
HttpWebRequest betrequest = (HttpWebRequest)HttpWebRequest.Create("https://www.bitdice.me/");
if (Prox != null)
betrequest.Proxy = Prox;
betrequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
betrequest.CookieContainer = new CookieContainer();
HttpWebResponse EmitResponse = (HttpWebResponse)betrequest.GetResponse();
string sEmitResponse = new StreamReader(EmitResponse.GetResponseStream()).ReadToEnd();
getcsrf(sEmitResponse);
getstream(sEmitResponse);
cookie = EmitResponse.Cookies["_csn_session"].Value;
if (Client != null)
Client.Close();
List<KeyValuePair<string, string>> headers = new List<KeyValuePair<string, string>>();
headers.Add(new KeyValuePair<string, string>("Cookie", "_csn_session=" + cookie));
Client = new WebSocket("wss://www.bitdice.me/stream/" + stream, "", null, headers, "dicebot", "http://bitdice.me", WebSocketVersion.Rfc6455);
Client.Opened += Client_Opened;
Client.Error += Client_Error;
Client.Closed += Client_Closed;
Client.MessageReceived += Client_MessageReceived;
Client.Open();
while (Client.State == WebSocketState.Connecting)
{
Thread.Sleep(100);
}
if (Client.State == WebSocketState.Open)
{
Client.Send("{\"jsonrpc\":\"2.0\",\"method\":\"user:update\",\"params\":{\"username\":\"" + Username + "\",\"user_seed\":\"1256e154283ea05b9538\",\"hide_bets_below\":\"0.0\",\"hide_other_bets\":false},\"id\":1}");
}
else
{
finishedlogin(false);
return false;
}
betrequest = (HttpWebRequest)HttpWebRequest.Create("https://www.bitdice.me/users/password");
betrequest.Method = "POST";
betrequest.CookieContainer = new CookieContainer();
string post = string.Format("user%5Bpassword%5D={0}&user%5Bpassword_confirmation%5D={0}", Password);
username = Username;
betrequest.ContentLength = post.Length;
if (Prox != null)
betrequest.Proxy = Prox;
betrequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
betrequest.CookieContainer.Add(new Cookie("_csn_session", cookie, "/", "bitdice.me"));
betrequest.Headers.Add("X-CSRF-Token", csrf);
using (var writer = new StreamWriter(betrequest.GetRequestStream()))
{
writer.Write(post);
}
EmitResponse = (HttpWebResponse)betrequest.GetResponse();
sEmitResponse = new StreamReader(EmitResponse.GetResponseStream()).ReadToEnd();
cookie = EmitResponse.Cookies["_csn_session"].Value;
betrequest = (HttpWebRequest)HttpWebRequest.Create("https://www.bitdice.me/");
if (Prox != null)
betrequest.Proxy = Prox;
betrequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
betrequest.CookieContainer = new CookieContainer();
betrequest.CookieContainer.Add(new Cookie("_csn_session", cookie, "/", "bitdice.me"));
EmitResponse = (HttpWebResponse)betrequest.GetResponse();
sEmitResponse = new StreamReader(EmitResponse.GetResponseStream()).ReadToEnd();
getDeposit(sEmitResponse);
getcsrf(sEmitResponse);
cookie = EmitResponse.Cookies["_csn_session"].Value;
getstream(sEmitResponse);
getcsrf(sEmitResponse);
getstream(sEmitResponse);
if (Client != null)
Client.Close();
headers = new List<KeyValuePair<string, string>>();
headers.Add(new KeyValuePair<string, string>("Cookie", "_csn_session=" + cookie));
Client = new WebSocket("wss://www.bitdice.me/stream/" + stream, "", null, headers, "dicebot", "http://bitdice.me", WebSocketVersion.Rfc6455);
Client.Opened += Client_Opened;
Client.Error += Client_Error;
Client.Closed += Client_Closed;
Client.MessageReceived += Client_MessageReceived;
Client.Open();
while (Client.State == WebSocketState.Connecting)
{
Thread.Sleep(100);
}
finishedlogin(Client.State == WebSocketState.Open);
return true;
}
//.........这里部分代码省略.........
示例13: TestWebSocketOrg
public void TestWebSocketOrg()
{
WebSocket webSocketClient = new WebSocket("ws://echo.websocket.org/");
webSocketClient.Error += new EventHandler<SuperSocket.ClientEngine.ErrorEventArgs>(webSocketClient_Error);
webSocketClient.AllowUnstrustedCertificate = true;
webSocketClient.Opened += new EventHandler(webSocketClient_Opened);
webSocketClient.Closed += new EventHandler(webSocketClient_Closed);
webSocketClient.MessageReceived += new EventHandler<MessageReceivedEventArgs>(webSocketClient_MessageReceived);
webSocketClient.Open();
if (!m_OpenedEvent.WaitOne(5000))
Assert.Fail("Failed to Opened session ontime");
Assert.AreEqual(WebSocketState.Open, webSocketClient.State);
for (var i = 0; i < 10; i++)
{
var message = Guid.NewGuid().ToString();
webSocketClient.Send(message);
if (!m_MessageReceiveEvent.WaitOne(5000))
{
Assert.Fail("Failed to get echo messsage on time");
break;
}
Console.WriteLine("Received echo message: {0}", m_CurrentMessage);
Assert.AreEqual(m_CurrentMessage, message);
}
webSocketClient.Close();
if (!m_CloseEvent.WaitOne(5000))
Assert.Fail("Failed to close session ontime");
Assert.AreEqual(WebSocketState.Closed, webSocketClient.State);
}
示例14: ConnectionTest
public void ConnectionTest()
{
WebSocket webSocketClient = new WebSocket(string.Format("{0}:{1}/websocket", Host, Port), subProtocol: "basic", version: m_Version);
webSocketClient.Error += new EventHandler<SuperSocket.ClientEngine.ErrorEventArgs>(webSocketClient_Error);
//webSocketClient.AllowUnstrustedCertificate = true;
webSocketClient.Opened += new EventHandler(webSocketClient_Opened);
webSocketClient.Closed += new EventHandler(webSocketClient_Closed);
webSocketClient.MessageReceived += new EventHandler<MessageReceivedEventArgs>(webSocketClient_MessageReceived);
webSocketClient.Open();
if (!m_OpenedEvent.WaitOne())
Assert.Fail("Failed to Opened session ontime");
Assert.AreEqual(WebSocketState.Open, webSocketClient.State);
webSocketClient.Close();
if (!m_CloseEvent.WaitOne(1000))
Assert.Fail("Failed to close session ontime");
Assert.AreEqual(WebSocketState.Closed, webSocketClient.State);
}
示例15: Main
static void Main(string[] args)
{
const string httpScheme = "https://";
const string wsScheme = "wss://";
const string url = "websocket.btcchina.com/socket.io/";
#region handshake
string polling = string.Empty;
try
{
WebClient wc = new WebClient();
polling = wc.DownloadString(httpScheme + url + "?transport=polling");
if (string.IsNullOrEmpty(polling))
{
Console.WriteLine("failed to download config");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
string config = polling.Substring(polling.IndexOf('{'), polling.IndexOf('}') - polling.IndexOf('{') + 1);
wsConfigHelper wsc = JsonConvert.DeserializeObject<wsConfigHelper>(config);
#endregion handshake
//set timers
pingTimeoutTimer = new Timer(_ =>
{
if (pong)
{
pong = false; //waiting for another ping
}
else
{
Console.WriteLine("Ping Timeout!");
}
}, null, Timeout.Infinite, Timeout.Infinite);
pingIntervalTimer = new Timer(_ =>
{
btc.Send(string.Format("{0}", (int)engineioMessageType.PING));
pingTimeoutTimer.Change(wsc.pingTimeout, Timeout.Infinite);
pong = false;
}, null, wsc.pingInterval, wsc.pingInterval);
//setup websocket connections and events
btc = new WebSocket(wsScheme + url + "?transport=websocket&sid=" + wsc.sid);
btc.Opened += btc_Opened;
btc.Error += btc_Error;
btc.MessageReceived += btc_MessageReceived;
btc.DataReceived += btc_DataReceived;
btc.Closed += btc_Closed;
btc.Open();
Console.ReadKey();
//close the connection.
btc.Send(string.Format("{0}{1}", (int)engineioMessageType.MESSAGE, (int)socketioMessageType.DISCONNECT));
Console.ReadKey();
pingIntervalTimer.Dispose();
pingTimeoutTimer.Dispose();
btc.Close();
}