本文整理汇总了C#中WebSocketSharp.WebSocket.Close方法的典型用法代码示例。如果您正苦于以下问题:C# WebSocket.Close方法的具体用法?C# WebSocket.Close怎么用?C# WebSocket.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WebSocketSharp.WebSocket
的用法示例。
在下文中一共展示了WebSocket.Close方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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;
}
示例2: GetClient
private static WebSocket GetClient(ServantConfiguration configuration)
{
var url = "ws://" + configuration.ServantIoUrl + "/Client?installationGuid=" + configuration.InstallationGuid + "&organizationGuid=" + configuration.ServantIoKey + "&servername=" + System.Environment.MachineName;
using (var ws = new WebSocket(url))
{
var serializer = new JavaScriptSerializer();
var pingTimer = new System.Timers.Timer(2000);
pingTimer.Elapsed += (sender, args) =>
{
ws.Ping();
};
pingTimer.Enabled = false;
ws.OnMessage += (sender, e) =>
{
var request = serializer.Deserialize<CommandRequest>(e.Data);
switch (request.Command)
{
case CommandRequestType.Unauthorized:
IsStopped = true;
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(DateTime.Now.ToLongTimeString() + ": Servant.io key was not recognized.");
Console.ResetColor();
ws.Close();
break;
case CommandRequestType.GetSites:
var sites = SiteManager.GetSites();
var result = serializer.Serialize(sites);
ws.Send(serializer.Serialize(new CommandResponse(request.Guid)
{
Guid = request.Guid,
Message = result,
Success = true
}));
break;
case CommandRequestType.UpdateSite:
var site = serializer.Deserialize<Site>(request.JsonObject);
var originalSite = SiteManager.GetSiteByName(request.Value);
if (originalSite == null)
{
ws.Send("not_found");
return;
}
originalSite.ApplicationPool = site.ApplicationPool;
originalSite.Name = site.Name;
originalSite.SiteState = site.SiteState;
originalSite.Bindings = site.Bindings;
originalSite.LogFileDirectory = site.LogFileDirectory;
originalSite.SitePath = site.SitePath;
originalSite.Bindings = site.Bindings;
SiteManager.UpdateSite(originalSite);
ws.Send(serializer.Serialize(new CommandResponse(request.Guid) { Message = "ok", Success = true}));
break;
case CommandRequestType.GetApplicationPools:
var appPools = SiteManager.GetApplicationPools();
ws.Send(serializer.Serialize(new CommandResponse(request.Guid) {Message = serializer.Serialize(appPools), Success = true}));
break;
case CommandRequestType.GetCertificates:
ws.Send(serializer.Serialize(new CommandResponse(request.Guid) {Message = serializer.Serialize(SiteManager.GetCertificates()), Success = true}));
break;
case CommandRequestType.StartSite:
var startSite = SiteManager.GetSiteByName(request.Value);
var startResult = SiteManager.StartSite(startSite);
ws.Send(serializer.Serialize(new CommandResponse(request.Guid) { Success = startResult == SiteStartResult.Started, Message = startResult.ToString() }));
break;
case CommandRequestType.StopSite:
var stopSite = SiteManager.GetSiteByName(request.Value);
SiteManager.StopSite(stopSite);
ws.Send(serializer.Serialize(new CommandResponse(request.Guid) { Success = true }));
break;
case CommandRequestType.RecycleApplicationPool:
var recycleSite = SiteManager.GetSiteByName(request.Value);
SiteManager.RecycleApplicationPoolBySite(recycleSite.IisId);
ws.Send(serializer.Serialize(new CommandResponse(request.Guid) { Message = "ok", Success = true }));
break;
case CommandRequestType.RestartSite:
var restartSite = SiteManager.GetSiteByName(request.Value);
SiteManager.RestartSite(restartSite.IisId);
ws.Send(serializer.Serialize(new CommandResponse(request.Guid) { Message = "ok", Success = true }));
break;
case CommandRequestType.DeleteSite:
var deleteSite = SiteManager.GetSiteByName(request.Value);
SiteManager.DeleteSite(deleteSite.IisId);
ws.Send(serializer.Serialize(new CommandResponse(request.Guid) { Message = "ok", Success = true }));
break;
case CommandRequestType.CreateSite:
var createSite = serializer.Deserialize<Site>(request.JsonObject);
var id = SiteManager.CreateSite(createSite);
ws.Send(serializer.Serialize(new CommandResponse(request.Guid) { Message = id.ToString(), Success = true }));
break;
}
};
//.........这里部分代码省略.........
示例3: Start
public void Start(string url = "ws://127.0.0.1:3000/socket.io/?EIO=4&transport=websocket")
{
ws = new WebSocket(url);
ws.OnOpen += (s, e) => RaiseEvent("open");
ws.OnError += (s, e)=> RaiseEvent("error");
ws.OnClose += (s, e) => RaiseEvent("close");
ws.OnMessage += OnMessage;
int reconnectDelay = 500;
base.Start(() =>
{
try {
if (ws.IsConnected)
{
Thread.Sleep(reconnectDelay);
}
else
{
ws.Connect();
}
}
catch(ThreadAbortException)
{
}
}
, ()=>
{
ws.Close();
});
}