go 服务端
使用到github.com/gorilla/websocket
go
package main
import (
"fmt"
"github.com/gorilla/websocket"
"log"
"net/http"
)
func main() {
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true //允许跨域
},
}
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) { //将所有的请求转换为websocket
upgrader.CheckOrigin = func(r *http.Request) bool {
return true
}
conn, err := upgrader.Upgrade(writer, request, nil)
if err != nil {
log.Fatal(err)
}
for {
t, msg, err := conn.ReadMessage()
if err != nil {
fmt.Printf("read error:%v\n", err)
break
}
fmt.Printf("[ws]recv count:%d recv:%s,msg type:%d\n", len(msg), string(msg), t)
}
})
err := http.ListenAndServe("127.0.0.1:8081", nil)
if err != nil {
log.Fatal(err)
}
}
C#客户端
C#操作websocket当客户端使用时使用System.Net.Websockets
这个命名空间下的ClientWebSocket
类
winform的设计界面如下:
代码如下:
csharp
public partial class Form1 : Form
{
CancellationTokenSource tokenSource = new CancellationTokenSource();
System.Net.WebSockets.ClientWebSocket client = new System.Net.WebSockets.ClientWebSocket();
public Form1()
{
InitializeComponent();
}
private async void button1_Click(object sender, EventArgs e)
{
try
{
var content=this.rtbContent.Text;
if (string.IsNullOrEmpty(content))
{
MessageBox.Show("请输入内容来发送");
return;
}
if (client.State != System.Net.WebSockets.WebSocketState.Open)
{
tokenSource = new CancellationTokenSource();
client = new System.Net.WebSockets.ClientWebSocket();
//client.Options.KeepAliveInterval = TimeSpan.FromSeconds(1);
await client.ConnectAsync(new Uri("ws://127.0.0.1:8081"), tokenSource.Token);
var bytes = new byte[10240];
var array = new ArraySegment<byte>(bytes);
Task.Factory.StartNew(async () =>
{
while (true)
{
try
{
var msg = await client.ReceiveAsync(array, tokenSource.Token);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
tokenSource.Cancel();
return;
}
}
});
}
var buffer = Encoding.UTF8.GetBytes(content);
await client.SendAsync(new ArraySegment<byte>(buffer), System.Net.WebSockets.WebSocketMessageType.Text, true, tokenSource.Token);
MessageBox.Show($"已发送{buffer.Length}个字节");
}
catch (Exception ee)
{
MessageBox.Show(ee.ToString());
}
}
}
注意:无论是客户端断开还是服务端断开,如果正在读取消息,则会触发错误。利用此机制可以很方便的处理重连问题。