一、新建项目
安装:SuperWebSocket 和 WebSocket4Net
二、Form1.cs
- using SuperWebSocket;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Net.WebSockets;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using WebSocket4Net;
-
- namespace WebSocket
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
-
- private void Form1_Load(object sender, EventArgs e)
- {
- }
-
- private void Log(string sa)
- {
- this.Invoke(new Action(() =>
- {
- txtContents.Text += sa + "\r\n";
- }));
- }
-
- /******************************服务端代码*******************************/
- private void btnStar_Click(object sender, EventArgs e)
- {
- WebSocketServer webSocketServer = new WebSocketServer();
- webSocketServer.NewSessionConnected += WebSocketServer_NewSessionConnected;
- webSocketServer.NewMessageReceived += WebSocketServer_NewMessageReceived;
- webSocketServer.SessionClosed += WebSocketServer_SessionClosed;
- if (!webSocketServer.Setup("127.0.0.1", 1234))
- {
- Log("设置服务监听失败!");
- }
- if (!webSocketServer.Start())
- {
- Log("启动服务监听失败!");
- }
- Log("启动服务监听成功!");
- }
- private void WebSocketServer_NewSessionConnected(WebSocketSession session)
- {
- Log("欢迎客户端: 加入");
- }
-
- private void WebSocketServer_NewMessageReceived(WebSocketSession session, string value)
- {
- Log("服务端收到客户端的数据 ==》" + value);
- }
-
- private void WebSocketServer_SessionClosed(WebSocketSession session, SuperSocket.SocketBase.CloseReason value)
- {
- Log("客户端:关闭,原因:");
- }
-
- /******************************客户端代码*******************************/
- public static WebSocket4Net.WebSocket webSocket4NetClient = null;
- public void ConnectWebsocket()
- {
- webSocket4NetClient = new WebSocket4Net.WebSocket("ws://127.0.0.1:1234");
- webSocket4NetClient.Opened += WebSocket4Net_Opened;
- webSocket4NetClient.Error += Websocket_Error;
- webSocket4NetClient.Closed += new EventHandler(Websocket_Closed);
- webSocket4NetClient.MessageReceived += WebSocket4Net_MessageReceived;
- webSocket4NetClient.Open();
- Thread thread = new Thread(ClientSendMessageToServer);
- thread.IsBackground = true;
- thread.Start();
- }
-
- public void ClientSendMessageToServer()
- {
- int i = 1;
- while (true)
- {
- webSocket4NetClient.Send("hi 你好!" + i++);
- Thread.Sleep(TimeSpan.FromSeconds(5));
- }
- }
-
- private void WebSocket4Net_MessageReceived(object sender, MessageReceivedEventArgs e)
- {
- }
-
- private void WebSocket4Net_Opened(object sender, EventArgs e)
- {
- webSocket4NetClient.Send("Client准备发送数据!");
- }
-
- private void Websocket_Error(object sender, EventArgs e)
- {
- Thread.Sleep(1000);
- if (webSocket4NetClient.State != WebSocket4Net.WebSocketState.Open && webSocket4NetClient.State != WebSocket4Net.WebSocketState.Connecting)
- {
- ConnectWebsocket();
- }
- }
-
- private void Websocket_Closed(object sender, EventArgs e)
- {
- Thread.Sleep(1000);
- if (webSocket4NetClient.State != WebSocket4Net.WebSocketState.Open && webSocket4NetClient.State != WebSocket4Net.WebSocketState.Connecting)
- {
- ConnectWebsocket();
- }
- }
- private void btnCon_Click(object sender, EventArgs e)
- {
- ConnectWebsocket();
- }
- }
- }
三、运行效果:
Js连接:
- <button onclick="print()">Print</button>
- <script type="text/javascript">
- var ws = new WebSocket("ws://127.0.0.1:1234");
- ws.onopen = function(){
- console.log("websocket连接成功");
- }
- ws.onmessage = function(e){
- console.log("收到数据");
- console.log(e.data);
- }
- ws.onclose = function(e){
- console.log("websocket已断开");
- }
- ws.onerror = function(e){
- console.log("websocket发生错误"+error);
- }
- function print(){
- ws.send("print")
- }
- </script>