一,什么是websocket
WebSocket是HTML5下一种新的协议(websocket协议本质上是一个基于tcp的协议)
它实现了浏览器与服务器全双工通信,能更好的节省服务器资源和带宽并达到实时通讯的目的
Websocket是一个持久化的协议
二,websocket的原理
websocket约定了一个通信的规范,通过一个握手的机制,客户端和服务器之间能建立一个类似tcp的连接,从而方便它们之间的通信
在websocket出现之前,web交互一般是基于http协议的短连接或者长连接
websocket是一种全新的协议,不属于http无状态协议,协议名为"ws"
三,websocket与http的关系
相同点:
都是基于tcp的,都是可靠性传输协议
都是应用层协议
不同点:
WebSocket是双向通信协议,模拟Socket协议,可以双向发送或接受信息
HTTP是单向的
WebSocket是需要浏览器和服务器握手进行建立连接的
而http是浏览器发起向服务器的连接,服务器预先并不知道这个连接
联系:
WebSocket在建立握手时,数据是通过HTTP传输的。但是建立之后,在真正传输时候是不需要HTTP协议的
总结(总体过程):
首先,客户端发起http请求,经过3次握手后,建立起TCP连接;http请求里存放WebSocket支持的版本号等信息,如:Upgrade、Connection、WebSocket-Version等;
然后,服务器收到客户端的握手请求后,同样采用HTTP协议回馈数据;
最后,客户端收到连接成功的消息后,开始借助于TCP传输信道进行全双工通信。
一、XAML代码
- <Window x:Class="Freed.WebSocket.Service.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:local="clr-namespace:Freed.WebSocket.Service"
- mc:Ignorable="d"
- Title="MainWindow" Height="450" Width="800">
- <Grid>
- <TextBox x:Name="textBox_Address" HorizontalAlignment="Left" Height="23" Margin="25,20,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="493"/>
- <Button x:Name="button" Content="启动" HorizontalAlignment="Left" Margin="552,23,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
- <TextBox x:Name="textBox_Content" HorizontalAlignment="Left" Height="294" Margin="31,72,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="672"/>
- <Button x:Name="but_Send" Content="发送" HorizontalAlignment="Left" Margin="633,23,0,0" VerticalAlignment="Top" Width="75" Click="but_Send_Click"/>
-
- Grid>
- Window>
二、后台代码
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using WebSocketSharp.Server;
-
- namespace Freed.WebSocket.Service
- {
- ///
- /// MainWindow.xaml 的交互逻辑
- ///
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- }
-
- private void button_Click(object sender, RoutedEventArgs e)
- {
- StartWebSocket(this.textBox_Address.Text);
- }
-
-
- WebSocketServer wssv;
- void StartWebSocket(string Ip)
- {
- wssv = new WebSocketServer(IPAddress.Parse(this.textBox_Address.Text), 9999);
- wssv.AddWebSocketService
("/Server"); - wssv.Start();
- if (wssv.IsListening)
- {
- MessageBox.Show(string.Format("Listening on port {0}, and providing WebSocket services:", wssv.Port));
- foreach (var path in wssv.WebSocketServices.Paths)
- MessageBox.Show(string.Format("- {0}", path));
- }
-
- }
-
- //发送
- private void but_Send_Click(object sender, RoutedEventArgs e)
- {
- wssv.WebSocketServices.Broadcast(this.textBox_Content.Text);
- }
- }
- }
2.1 ServerSharp
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using WebSocketSharp;
- using WebSocketSharp.Server;
-
- namespace Freed.WebSocket.Service
- {
- public class ServerSharp : WebSocketBehavior
- {
- int count = 0;
- protected override void OnClose(CloseEventArgs e)
- {
- count++;
- Task tk = new Task(() => {
- MessageBox.Show("客户端关闭连接");
-
- });
- tk.Start();
- }
-
- protected override void OnError(ErrorEventArgs e)
- {
- Task tk = new Task(() => {
- MessageBox.Show($"客户端连接异常:{e.Message}");
- });
- tk.Start();
- }
-
- protected override void OnMessage(MessageEventArgs e)
- {
- Task tk = new Task(() => {
- Send("接收到消息:" + e.Data);
- });
- tk.Start();
- }
- }
- }