本文整理汇总了C#中WebSocketSharp.WebSocket类的典型用法代码示例。如果您正苦于以下问题:C# WebSocket类的具体用法?C# WebSocket怎么用?C# WebSocket使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
WebSocket类属于WebSocketSharp命名空间,在下文中一共展示了WebSocket类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ScarletMetrics
/* WS */
public ScarletMetrics()
{
metricsws = new WebSocket("ws://" + MetricsWSURL + ":" + MetricsWSPort);
metricsws.Connect();
metricsws.OnOpen += (sender, e) =>
{
// Metrics - On Connection to the Metrics WS Reporting Server
// Need to include username / IP in here as well.
JsonObject jsonMessage = new JsonObject()
.add("type", "metrics")
.add("message", "connected");
metricsws.Send(jsonMessage.ToString());
};
metricsws.OnMessage += (sender, e) =>
{
};
metricsws.OnClose += (sender, e ) =>
metricsws.Connect();
metricsws.OnError += (sender, e) =>
metricsws.Connect();
}
示例2: ConnectToWebSocket
public int ConnectToWebSocket()
{
ws = new WebSocket(_Address);
timer = new System.Windows.Forms.Timer();
timer.Interval = 1500;
timer.Tick += (sender, e) =>
{
if (ws != null)
ws.Send(JsonConvert.SerializeObject(new { type = "getframe" }));
};
ws.OnMessage += Ws_OnMessage;
ws.OnOpen += (sender, e) =>
{
//ws.Send(JsonConvert.SerializeObject(new { type = "getinfo" }));
string msg = "{\"type\":\"getinfo\"}";
ws.Send(msg);
};
ws.OnError += (sender, e) =>
{
MessageBox.Show("Error during Websocket Connection: " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
ws.Close();
ws = null;
this.Dispose();
};
ws.Connect();
if (Connected != null)
Connected();
timer.Start();
return 0;
}
示例3: PreStart
protected override void PreStart()
{
var self = Self;
_socket = new WebSocket(_endpoint);
_socket.OnOpen += (sender, args) =>
{
self.Tell(new ConnectionOpened());
};
_socket.OnMessage += (sender, args) =>
{
self.Tell(new MessageReceived(args));
};
_socket.OnError += (sender, args) =>
{
self.Tell(new ErrorOccurred(args.Message, args.Exception));
};
_socket.OnClose += (sender, args) =>
{
self.Tell(new ConnectionClosed());
};
_socket.SetCookie(new Cookie("ClientId", _clientId));
_socket.ConnectAsync();
}
示例4: ChatController
//needs to implemented from API
/// <summary>
///
/// </summary>
/// <param name="MessageReceived"></param>
public ChatController(Model m)
{
ws = new WebSocket("ws://127.0.0.1:8005/chat");
ws.OnMessage += (s, e) => GotPacket(e.Data);
ws.Connect();
this.m = m;
}
示例5: HearbeatTransmitter
public HearbeatTransmitter(WebSocket socket, TimeSpan heartBeatInterval, Func<Dictionary<string, object>> onSendingHearbeat)
{
_socket = socket;
_heartBeatInterval = heartBeatInterval;
_onSendingHearbeat = onSendingHearbeat;
Receive<string>(message =>
{
if (socket.ReadyState == WebSocketState.Open)
{
var heartbeat = onSendingHearbeat();
heartbeat["$$Timestamp"] = DateTime.UtcNow;
var jsonPayload = JsonConvert.SerializeObject(new
{
Type = "Heartbeat",
Payload = heartbeat
});
socket.Send(jsonPayload);
Context.System.Scheduler.ScheduleTellOnce(heartBeatInterval, Self, "SendHeartbeat", Self);
}
}, msg => msg == "SendHeartbeat");
}
示例6: Start
public void Start()
{
var model = _modelService.getPendingModel();
var thisChan = model.findByPath(_context.getPath());
// 1 - récupérer l'ensemble des ports input (provided) qui ne sont pas à l'intérieur du noeud courrant
// 2 - lancer un consumer dessus avec en suffixe d'url le path vers le port en question
foreach (var remoteInputPort in _remoteInputPorts)
{
var websocket = new WebSocket(GetChanURI(remoteInputPort.getPath()));
websocket.Connect();
_remoteInputPortWebsockets.Add(remoteInputPort.getPath(), websocket);
}
foreach (var inputPort in _inputPorts)
{
var websocket = new WebSocket(GetChanURI(inputPort.getPath()));
websocket.OnMessage += (sender, data) =>
{
inputPort.send(data.Data, null);
};
websocket.Connect();
_inputPortWebsockets.Add(inputPort.getPath(), websocket);
}
}
示例7: NetworkInterface
public NetworkInterface(string address, ContentManager c)
{
sock = new WebSocket(address);
Content = c;
connected = false;
sock.OnMessage += (sender, e) =>
{
byte[] r = e.RawData;
string s = System.Text.Encoding.UTF8.GetString(r);
doNetworkInput(s);
};
sock.OnOpen += (sender, e) =>
{
connected = true;
Console.WriteLine("Connected");
onReady();
};
sock.OnClose += (sender, e) =>
{
Console.WriteLine("Ran onclose");
connected = false;
};
sock.OnError += (sender, e) =>
{
if (e.Message == "An error has occurred while connecting.")
{
connected = false;
onReady();
}
};
sock.ConnectAsync();
}
示例8: MessageReceiver
public MessageReceiver(WebSocket client, IMessageDecoder decoder, IMessageDecompressor decompressor)
{
mDecoder = decoder;
mDecompressor = decompressor;
mClient = client;
mClient.OnMessage += OnMessage;
}
示例9: Awake
public void Awake()
{
encoder = new Encoder();
decoder = new Decoder();
parser = new Parser();
handlers = new Dictionary<string, List<Action<SocketIOEvent>>>();
ackList = new List<Ack>();
sid = null;
packetId = 0;
ws = new WebSocket(url);
ws.OnOpen += OnOpen;
ws.OnMessage += OnMessage;
ws.OnError += OnError;
ws.OnClose += OnClose;
wsConnected = false;
eventQueueLock = new object();
eventQueue = new Queue<SocketIOEvent>();
ackQueueLock = new object();
ackQueue = new Queue<Packet>();
connected = false;
#if SOCKET_IO_DEBUG
if(debugMethod == null) { debugMethod = Debug.Log; };
#endif
}
示例10: ConnectToServer
public void ConnectToServer(string url, string key)
{
_ws = new WebSocket("ws://"+url);
_ws.Origin = "http://agar.io";
_ws.Log.Level = LogLevel.None;
_ws.OnMessage += (sender, e) => {
_dataMutex.WaitOne();
_dataQueue.Enqueue(e.RawData);
_dataMutex.ReleaseMutex();
};
_ws.OnError += (sender, e) => {
Console.WriteLine(e.Message);
};
_ws.OnOpen += (sender, e) =>
{
_open = true;
this.SendHandShake(key);
Console.WriteLine("Connection opened to " + url + " - " + key);
};
_ws.Connect();
}
示例11: Connect
private void Connect(IPAddress address, int port)
{
if (this.ws != null
&& (this.ws.ReadyState == WebSocketState.Connecting || this.ws.ReadyState == WebSocketState.Open))
return;
if (this.ws != null)
{
this.ws.Close();
this.ws = null;
}
this.ws = new WebSocket("ws://" + address + ":" + port + "/remote");
this.ws.OnMessage += (sender, args) => { this.OnReceive(args.Data); };
this.ws.OnError += (sender, args) =>
{
if (!this.ws.IsAlive)
{
this.ws.Close();
this.ws.Connect();
}
};
this.ws.Connect();
if (this.ws.ReadyState == WebSocketState.Open)
{
this.Authenticate();
this.refetchTimer = new Timer(this.Refetch, null, 0, 500);
}
}
示例12: Main
public static void Main(string[] args)
{
if (args.Length != 1) {
Console.Error.WriteLine ("ERROR: need a valid Websocket URL");
Environment.Exit (-1);
}
using (var ws = new WebSocket (args[0])) {
Int64 syncCtr = 0;
ws.OnMessage += (sender, e) => {
Console.WriteLine (e.Data);
Interlocked.Decrement (ref syncCtr);
};
ws.Connect ();
if (!ws.IsAlive) {
Console.Error.WriteLine ("ERROR: Could not connect to Websocket Server {0}", args[0]);
Environment.Exit(-2);
}
var input = "";
while (string.Compare (input.Trim (), "shutdown", true) != 0) {
if (!string.IsNullOrWhiteSpace (input)) {
ws.Send (input.Trim());
Interlocked.Increment (ref syncCtr);
while (Interlocked.Read (ref syncCtr) > 0) {
Thread.Sleep (1);
}
}
Console.Write ("Type in a message or 'shutdown' to quit: ");
input = Console.ReadLine ();
} // Longevity loop
} // using
}
示例13: ThisAddIn_Startup
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Excel.Worksheet activeWorksheet = ((Excel.Worksheet)Application.ActiveSheet);
Excel.Range firstRow = activeWorksheet.get_Range("A1");
firstRow.EntireRow.Insert(Excel.XlInsertShiftDirection.xlShiftDown);
Excel.Range newFirstRow = activeWorksheet.get_Range("A1");
newFirstRow.Value2 = "lETS START THIS THING";
//lets start this thing
using (var ws = new WebSocket("ws://dumbsocket.herokuapp.com"))
{
ws.OnError += (error_sender, error_e) => {
Console.Write("socket error");
//SOME BROKE SHIT
};
ws.OnOpen += (open_sender, open_e) => {
Console.Write("sockets open");
//windows open
};
ws.OnClose += (close_sender, close_e) => {
Console.Write("socket closed");
};
ws.OnMessage += (socket_sender, socket_e) =>
{
newFirstRow.Value2 = "This is coming from the websocket" + socket_e.Data;
};
ws.Connect();
}
}
示例14: Connect
public void Connect(string url)
{
DisposeCurrentConnection();
if (IsConnected)
{
Close();
}
var ws = new WebSocket(url);
ws = new WebSocket(url);
ws.OnOpen += OnOpen;
ws.OnMessage += OnMessage;
ws.OnError += OnError;
ws.OnClose += OnClose;
_isWsConnected = false;
_socketWorker = new WebSocketWorker(ws, RunSocketThread);
_socketWorker.Start();
_pingWorker = new WebSocketWorker(ws, RunPingThread);
_pingWorker.Start();
_ws = ws;
}
示例15: ReConnectWebSocket
public void ReConnectWebSocket(string Address)
{
if( Socket != null )
Socket.Close();
Socket = null;
ConnectWebSocket(Address);
}