代码拉取完成,页面将自动刷新
using Com.Scm.Printer.Cfg;
using Com.Scm.Printer.Dvo;
using Fleck;
using System;
using System.Text.Json;
using System.Windows.Forms;
namespace Com.Scm.Printer
{
public partial class MainForm : Form
{
public const string FORMAT_DATETIME = "yyyy-MM-dd HH:mm:ss";
private WebSocketServer _Socket;
private ClientConfig _ClientConfig;
private RemoteConfig _RemoteConfig;
public MainForm()
{
InitializeComponent();
LoadLocaleCfg();
LoadRemoteCfg();
Start();
}
/// <summary>
/// 客户端配置
/// </summary>
private void LoadLocaleCfg()
{
_ClientConfig = new ClientConfig();
_ClientConfig.LoadDef();
}
/// <summary>
/// 服务端配置
/// </summary>
private void LoadRemoteCfg()
{
_RemoteConfig = new RemoteConfig();
_RemoteConfig.LoadDef();
}
/// <summary>
/// 启动服务
/// </summary>
private void Start()
{
if (_Socket != null)
{
return;
}
var url = $"ws://{_ClientConfig.Host}:{_ClientConfig.Port}";
_Socket = new WebSocketServer(url);
_Socket.RestartAfterListenError = true;
_Socket.Start(socket =>
{
socket.OnOpen = SocketOpend;
socket.OnClose = SocketClosed;
socket.OnMessage = message => OnMessage(socket, message);
});
}
private void SocketOpend()
{
Log("事件:Socket Opend!");
}
private void SocketClosed()
{
Log("事件:Socket Closed!");
}
private void OnMessage(IWebSocketConnection connect, string message)
{
Log("接收:" + message);
if (string.IsNullOrEmpty(message))
{
FeedError(connect, "请求内容为空!");
return;
}
try
{
var request = AsJsonObject<PrintRequest>(message);
if (request == null)
{
FeedError(connect, "无效的请求内容!");
return;
}
var task = new PrintTask();
task.id = request.id;
task.types = request.types;
task.printer = request.printer;
task.handle = HandleEnum.Todo;
task.times = 0;
task.create_time = DateTime.Now.ToString(FORMAT_DATETIME);
ucTask1.Add(task);
var response = new PrintResponse();
response.SetSuccess("打成成功!");
connect.Send(ToJsonString(response));
}
catch (Exception ex)
{
FeedError(connect, "系统处理异常-" + ex.Message);
}
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (_Socket != null)
{
_Socket.Dispose();
}
}
private void Log(string message)
{
ucLogs1.Log(message);
}
private void FeedError(IWebSocketConnection connect, string message)
{
var response = new PrintResponse();
response.SetFailure(message);
var feed = ToJsonString(response);
ucLogs1.Log("回馈:" + feed);
connect.Send(feed);
}
private string ToJsonString(object obj)
{
return JsonSerializer.Serialize(obj);
}
private T AsJsonObject<T>(string json)
{
return JsonSerializer.Deserialize<T>(json);
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。