From 6c62b03bcaf50b0d2dcf85e2accd374283ad5e2e Mon Sep 17 00:00:00 2001
From: yl <ykxkd@outlook.com>
Date: 星期五, 16 九月 2022 23:15:46 +0800
Subject: [PATCH] 班组工资报表开发、人员工资明细报表开发、不良明细报表开发、品质异常报表开发、维修明细报表开发
---
VueWebApi/Controllers/SendController.cs | 101 ++++++++++++++++++++++++++++++++++++++++----------
1 files changed, 81 insertions(+), 20 deletions(-)
diff --git a/VueWebApi/Controllers/SendController.cs b/VueWebApi/Controllers/SendController.cs
index 21cc605..6b4c203 100644
--- a/VueWebApi/Controllers/SendController.cs
+++ b/VueWebApi/Controllers/SendController.cs
@@ -1,48 +1,109 @@
锘縰sing System;
using System.Collections.Generic;
-using System.Linq;
+using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.WebSockets;
+using System.Runtime.Serialization;
+using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
+using System.Web;
using System.Web.Http;
+using System.Web.WebSockets;
using VueWebApi.Tools;
namespace VueWebApi.Controllers
{
- [RoutePrefix(prefix: "api/Send")]
+ [RoutePrefix(prefix: "Send")]
[ControllerGroup("娑堟伅鎻愰啋", "鍦ㄧ嚎鎺ュ彛")]
public class SendController : ApiController
{
private readonly ClientWebSocket webSocket = new ClientWebSocket();
private readonly CancellationToken _cancellation = new CancellationToken();
+ /// <summary>
+ /// 鐧诲綍鐢ㄦ埛寤虹珛WebSocket鏈嶅姟杩炴帴
+ /// </summary>
+ /// <param name="clientName">鐧诲綍鐢ㄦ埛淇℃伅</param>
+ /// <returns></returns>
[HttpGet]
- public async Task SendMsg(string msg)
+ public HttpResponseMessage GetConnect(string clientName)
{
- await webSocket.ConnectAsync(new Uri("ws://localhost:8001"), CancellationToken.None);
- var sendBytes = Encoding.UTF8.GetBytes(msg);//鍙戦�佺殑鏁版嵁
- var bsend = new ArraySegment<byte>(sendBytes);
- await webSocket.SendAsync(bsend, WebSocketMessageType.Binary, true, _cancellation);
- await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "1", _cancellation);
- webSocket.Dispose();
+ HttpContext.Current.AcceptWebSocketRequest(ProcessRequest); //鍦ㄦ湇鍔″櫒绔帴鍙梂eb Socket璇锋眰锛屼紶鍏ョ殑鍑芥暟浣滀负Web Socket鐨勫鐞嗗嚱鏁帮紝寰匴eb Socket寤虹珛鍚庤鍑芥暟浼氳璋冪敤锛屽湪璇ュ嚱鏁颁腑鍙互瀵筗eb Socket杩涜娑堟伅鏀跺彂
+
+ return Request.CreateResponse(HttpStatusCode.SwitchingProtocols); //鏋勯�犲悓鎰忓垏鎹㈣嚦Web Socket鐨凴esponse.
+ }
+ ///璁板綍瀹㈡埛绔�
+ private static List<WebSocket> _sockets = new List<WebSocket>();
+ ///鎺ュ彈淇℃伅鍜屽彂閫佷俊鎭�
+ public async Task ProcessRequest(AspNetWebSocketContext context)
+ {
+ try
+ {
+ var socket = context.WebSocket;//浼犲叆鐨刢ontext涓湁褰撳墠鐨剋eb socket瀵硅薄
+ _sockets.Add(socket);//姝ゅ灏唚eb socket瀵硅薄鍔犲叆涓�涓潤鎬佸垪琛ㄤ腑
+
+ //杩涘叆涓�涓棤闄愬惊鐜紝褰搘eb socket close鏄惊鐜粨鏉�
+ while (true)
+ {
+ var buffer = new ArraySegment<byte>(new byte[1024]);
+ var receivedResult = await socket.ReceiveAsync(buffer, CancellationToken.None);//瀵箇eb socket杩涜寮傛鎺ユ敹鏁版嵁
+ if (receivedResult.MessageType == WebSocketMessageType.Close)
+ {
+ await socket.CloseAsync(WebSocketCloseStatus.Empty, string.Empty, CancellationToken.None);//濡傛灉client鍙戣捣close璇锋眰锛屽client杩涜ack
+ _sockets.Remove(socket);
+ break;
+ }
+
+ if (socket.State == System.Net.WebSockets.WebSocketState.Open)
+ {
+ string recvMsg = Encoding.UTF8.GetString(buffer.Array, 0, receivedResult.Count);
+ var recvBytes = Encoding.UTF8.GetBytes(recvMsg);
+ var sendBuffer = new ArraySegment<byte>(recvBytes);
+ foreach (var innerSocket in _sockets)//褰撴帴鏀跺埌鏂囨湰娑堟伅鏃讹紝瀵瑰綋鍓嶆湇鍔″櫒涓婃墍鏈墂eb socket杩炴帴杩涜骞挎挱
+ {
+ if (innerSocket != socket)
+ {
+ await innerSocket.SendAsync(sendBuffer, WebSocketMessageType.Text, true, CancellationToken.None);
+ }
+ }
+ }
+ }
+ }
+ catch (Exception ex)
+ {
+ throw new Exception(ex.Message);
+ }
}
- //http://localhost:port/api/Send/Init
- [HttpGet]
- public string Init()
- {
- TestSocket.Instance.socketServer();
- return "success";
- }
- [HttpGet]
- public string Msg(string userid, string msg)
+ //瀹㈡埛绔彂閫佹秷鎭�
+ private async void SendMsg(object data)
{
- var _msg = TestSocket.Instance.Send(userid, msg);
- return _msg;
+ try
+ {
+ await webSocket.ConnectAsync(new Uri("wss://121.196.36.24:8001/api/Send?clientName=" + 123), _cancellation);
+ var sendBytes = ObjectToBytes(data);//鍙戦�佺殑鏁版嵁
+ var bsend = new ArraySegment<byte>(sendBytes);
+ await webSocket.SendAsync(bsend, WebSocketMessageType.Binary, true, _cancellation);
+ await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "1", _cancellation);
+ webSocket.Dispose();//璁板緱涓�瀹氳閲婃斁涓嶇劧鏈嶅姟绔繕浜х敓寰堝杩炴帴
+ }
+ catch (Exception ex)
+ {
+ throw new Exception(ex.Message);
+ }
+ }
+ public static byte[] ObjectToBytes(object obj)
+ {
+ using (MemoryStream ms = new MemoryStream())
+ {
+ IFormatter formatter = new BinaryFormatter();
+ formatter.Serialize(ms, obj);
+ return ms.GetBuffer();
+ }
}
}
}
--
Gitblit v1.9.3