1 Star 2 Fork 1

OpenSCM/Scm.Printer

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
MainForm.cs 3.77 KB
一键复制 编辑 原始数据 按行查看 历史
小阿木 提交于 2年前 . 项目初始化。
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);
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/openscm/scm.printer.git
git@gitee.com:openscm/scm.printer.git
openscm
scm.printer
Scm.Printer
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385