1 Star 2 Fork 3

夏季的风 / go-webSocket服务端

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Service.go 3.85 KB
一键复制 编辑 原始数据 按行查看 历史
lingbinbin 提交于 2021-09-17 09:18 . *升级任务处理池
package main
import (
"bytes"
"fmt"
"gitee.com/ling-bin/netwebSocket/netInterface"
"gitee.com/ling-bin/netwebSocket/netService"
"github.com/gorilla/websocket"
"log"
"net"
"net/http"
"os"
"runtime"
"strconv"
"time"
)
func main() {
addrs, err := net.InterfaceAddrs()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
port := []string{"7010"} //监听端口
addrAry := make([]string, 0, 5)
for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
for _, val := range port {
addrAry = append(addrAry, ipnet.IP.String()+":"+val)
}
}
}
}
for _, val := range port {
addrAry = append(addrAry, fmt.Sprint("127.0.0.1:", val))
}
config := netService.DefaultConfig("ws", addrAry, nil)
service := netService.NewService(config)
//连接验证
service.SetOnConnAuth(func(conn netInterface.IConnection, w http.ResponseWriter, r *http.Request) bool {
return true
})
//连接启动
service.SetOnConnStart(func(connection netInterface.IConnection) {
connId := strconv.FormatUint(connection.GetConnId(), 10)
fmt.Println("[", connection.GetRemoteAddr().String(), "]新连接:", connId)
})
//连接关闭
service.SetOnConnStop(func(connection netInterface.IConnection) {
connId := strconv.FormatUint(connection.GetConnId(), 10)
fmt.Println("[", connection.GetRemoteAddr().String(), "]连接关闭:", connId)
})
//第一包数据[调用后会调用SetOnReceiveCompleted,在同一协程执行]
service.SetOnOneReceive(func(connection netInterface.IConnection, msgType int, data []byte) {
connId := strconv.FormatUint(connection.GetConnId(), 10)
fmt.Println("[", connection.GetRemoteAddr().String(), " ", connection.GetLocalAddr().String(), "][", connId, "]连接上传第一次上数据:", len(data), "---", string(data))
})
//接收到的数据
service.SetOnReceive(func(connection netInterface.IConnection, msgType int, data []byte) {
connId := strconv.FormatUint(connection.GetConnId(), 10)
fmt.Println("[", connection.GetRemoteAddr().String(), " ", connection.GetLocalAddr().String(), "][", connId, "]客户端路径[", connection.GetPath(), "]连接上传新数据:", len(data), "---", string(data))
//回复
by := bytes.Buffer{}
by.Write([]byte("收到 => "))
by.Write(data)
connection.SendData(websocket.TextMessage, by.Bytes(),"")
})
//数据回复成功
service.SetOnReply(func(connection netInterface.IConnection, msgType int, data []byte, isOk bool,cmdCode string, param interface{}, err error) {
connId := strconv.FormatUint(connection.GetConnId(), 10)
fmt.Println("[下行回调][", connection.GetRemoteAddr().String(), "][", connId, "]:", len(data), "---", string(data))
if !isOk {
log.Println("数据下发异常:", err)
}
})
//日志
service.SetLogHandle(func(level netInterface.ErrLevel, msg ...interface{}) {
switch level {
case netInterface.Fatal:
log.Panicln("[致命]", msg)
break
case netInterface.Error:
log.Println("[错误]", msg)
break
case netInterface.Warn:
log.Println("[警告]", msg)
break
case netInterface.Info:
log.Println("[消息]", msg)
break
}
})
service.Start()
go func() {
for {
receiveWorkerPool := service.GetReceiveWorkerPool()
queueCount, totalCount, timeCount := receiveWorkerPool.GetTaskPoolInfo()
replyWorkerPool := service.GetReplyWorkerPool()
rqueueCount, rtotalCount, rtimeCount := replyWorkerPool.GetTaskPoolInfo()
log.Println(
"启动时间:", service.GetStartTime().Format("2006-01-02 15:04:05"),
" 连接数:", service.GetConnMgr().Count(),
" [接收]处理数:", totalCount,
" 剩余数:", queueCount,
" 处理超时数:", timeCount,
" [回复]处理数:", rtotalCount,
" 剩余数:", rqueueCount,
" 处理超时数:", rtimeCount,
" 协程数:", runtime.NumGoroutine())
time.Sleep(time.Second * 1)
}
}()
select {}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/ling-bin/netwebSocket.git
git@gitee.com:ling-bin/netwebSocket.git
ling-bin
netwebSocket
go-webSocket服务端
v1.3.4

搜索帮助

344bd9b3 5694891 D2dac590 5694891