• winform开启websocket


    一、新建项目

    安装:SuperWebSocket  和 WebSocket4Net

    二、Form1.cs

    1. using SuperWebSocket;
    2. using System;
    3. using System.Collections.Generic;
    4. using System.ComponentModel;
    5. using System.Data;
    6. using System.Drawing;
    7. using System.IO;
    8. using System.Linq;
    9. using System.Net;
    10. using System.Net.WebSockets;
    11. using System.Text;
    12. using System.Threading;
    13. using System.Threading.Tasks;
    14. using System.Windows.Forms;
    15. using WebSocket4Net;
    16. namespace WebSocket
    17. {
    18. public partial class Form1 : Form
    19. {
    20. public Form1()
    21. {
    22. InitializeComponent();
    23. }
    24. private void Form1_Load(object sender, EventArgs e)
    25. {
    26. }
    27. private void Log(string sa)
    28. {
    29. this.Invoke(new Action(() =>
    30. {
    31. txtContents.Text += sa + "\r\n";
    32. }));
    33. }
    34. /******************************服务端代码*******************************/
    35. private void btnStar_Click(object sender, EventArgs e)
    36. {
    37. WebSocketServer webSocketServer = new WebSocketServer();
    38. webSocketServer.NewSessionConnected += WebSocketServer_NewSessionConnected;
    39. webSocketServer.NewMessageReceived += WebSocketServer_NewMessageReceived;
    40. webSocketServer.SessionClosed += WebSocketServer_SessionClosed;
    41. if (!webSocketServer.Setup("127.0.0.1", 1234))
    42. {
    43. Log("设置服务监听失败!");
    44. }
    45. if (!webSocketServer.Start())
    46. {
    47. Log("启动服务监听失败!");
    48. }
    49. Log("启动服务监听成功!");
    50. }
    51. private void WebSocketServer_NewSessionConnected(WebSocketSession session)
    52. {
    53. Log("欢迎客户端: 加入");
    54. }
    55. private void WebSocketServer_NewMessageReceived(WebSocketSession session, string value)
    56. {
    57. Log("服务端收到客户端的数据 ==》" + value);
    58. }
    59. private void WebSocketServer_SessionClosed(WebSocketSession session, SuperSocket.SocketBase.CloseReason value)
    60. {
    61. Log("客户端:关闭,原因:");
    62. }
    63. /******************************客户端代码*******************************/
    64. public static WebSocket4Net.WebSocket webSocket4NetClient = null;
    65. public void ConnectWebsocket()
    66. {
    67. webSocket4NetClient = new WebSocket4Net.WebSocket("ws://127.0.0.1:1234");
    68. webSocket4NetClient.Opened += WebSocket4Net_Opened;
    69. webSocket4NetClient.Error += Websocket_Error;
    70. webSocket4NetClient.Closed += new EventHandler(Websocket_Closed);
    71. webSocket4NetClient.MessageReceived += WebSocket4Net_MessageReceived;
    72. webSocket4NetClient.Open();
    73. Thread thread = new Thread(ClientSendMessageToServer);
    74. thread.IsBackground = true;
    75. thread.Start();
    76. }
    77. public void ClientSendMessageToServer()
    78. {
    79. int i = 1;
    80. while (true)
    81. {
    82. webSocket4NetClient.Send("hi 你好!" + i++);
    83. Thread.Sleep(TimeSpan.FromSeconds(5));
    84. }
    85. }
    86. private void WebSocket4Net_MessageReceived(object sender, MessageReceivedEventArgs e)
    87. {
    88. }
    89. private void WebSocket4Net_Opened(object sender, EventArgs e)
    90. {
    91. webSocket4NetClient.Send("Client准备发送数据!");
    92. }
    93. private void Websocket_Error(object sender, EventArgs e)
    94. {
    95. Thread.Sleep(1000);
    96. if (webSocket4NetClient.State != WebSocket4Net.WebSocketState.Open && webSocket4NetClient.State != WebSocket4Net.WebSocketState.Connecting)
    97. {
    98. ConnectWebsocket();
    99. }
    100. }
    101. private void Websocket_Closed(object sender, EventArgs e)
    102. {
    103. Thread.Sleep(1000);
    104. if (webSocket4NetClient.State != WebSocket4Net.WebSocketState.Open && webSocket4NetClient.State != WebSocket4Net.WebSocketState.Connecting)
    105. {
    106. ConnectWebsocket();
    107. }
    108. }
    109. private void btnCon_Click(object sender, EventArgs e)
    110. {
    111. ConnectWebsocket();
    112. }
    113. }
    114. }

    三、运行效果:

    Js连接:

    1. <button onclick="print()">Print</button>
    2. <script type="text/javascript">
    3. var ws = new WebSocket("ws://127.0.0.1:1234");
    4. ws.onopen = function(){
    5. console.log("websocket连接成功");
    6. }
    7. ws.onmessage = function(e){
    8. console.log("收到数据");
    9. console.log(e.data);
    10. }
    11. ws.onclose = function(e){
    12. console.log("websocket已断开");
    13. }
    14. ws.onerror = function(e){
    15. console.log("websocket发生错误"+error);
    16. }
    17. function print(){
    18. ws.send("print")
    19. }
    20. </script>

  • 相关阅读:
    linux安装java环境
    python基础-异常处理
    【022】Vue+Springboot+mysql汽车销售系统课设(含源码、数据库、运行教程、实验报告)
    一文吃透JavaScript程序控制流与函数
    XMLDecoder解析流程分析
    SpringBoot注解
    Skywalking UI使用攻略
    cookie和session
    如何选择合适的 AWS 区域对网站性能和成本?
    Arduino开发实例-SR04T/SR04M 防水超声波传感器驱动
  • 原文地址:https://blog.csdn.net/xiaoxionglove/article/details/125417492