websocket

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 15, 2023 License: MIT Imports: 17 Imported by: 2

Documentation

Index

Constants

View Source
const (
	DefaultAntsPoolSize = 1024 * 1024

	ConnMaxIdleSeconds int64 = 600 //连接空闲超过600s将自动关闭
)

Variables

View Source
var ClientTemplate = template.Must(template.New("").Parse(`
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script>  
window.addEventListener("load", function(evt) {
    var output = document.getElementById("output");
    var input = document.getElementById("input");
    var ws;
    var print = function(message) {
        var d = document.createElement("div");
        d.innerHTML = message;
        output.appendChild(d);
    };
    document.getElementById("open").onclick = function(evt) {
        if (ws) {
            return false;
        }
        ws = new WebSocket("{{.}}");
        ws.onopen = function(evt) {
            print("OPEN");
        }
        ws.onclose = function(evt) {
            print("CLOSE");
            ws = null;
        }
        ws.onmessage = function(evt) {
            print("RESPONSE: " + evt.data);
        }
        ws.onerror = function(evt) {
            print("ERROR: " + evt.data);
        }
        return false;
    };
    document.getElementById("send").onclick = function(evt) {
        if (!ws) {
            return false;
        }
        print("SEND: " + input.value);
        ws.send(input.value);
        return false;
    };
    document.getElementById("close").onclick = function(evt) {
        if (!ws) {
            return false;
        }
        ws.close();
        return false;
    };
});
</script>
</head>
<body>
<table>
<tr><td valign="top" width="50%">
<p>Click "Open" to create a connection to the server, 
"Send" to send a message to the server and "Close" to close the connection. 
You can change the message and send multiple times.
<p>
<form>
<button id="open">Open</button>
<button id="close">Close</button>
<p><input id="input" type="text" value="Hello world!">
<button id="send">Send</button>
</form>
</td><td valign="top" width="50%">
<div id="output"></div>
</td></tr></table>
</body>
</html>

`))

Functions

func EchoDataHandler

func EchoDataHandler(param *DataHandlerParam)

简单的echo server

Types

type ConnCloseHandleFunc

type ConnCloseHandleFunc func(wsConn *GnetUpgraderConn)

连接关闭处理函数

type DataHandler

type DataHandler func(param *DataHandlerParam)

type DataHandlerParam

type DataHandlerParam struct {
	Request []byte

	OpCode ws.OpCode

	Writer io.Writer

	WSConn *GnetUpgraderConn //升级后的连接

	Server *WebSocketServer
}

type GnetUpgraderConn

type GnetUpgraderConn struct {
	GnetConn gnet.Conn

	UniqId string //连接的全局唯一id ?

	LastActiveTs int64 //上次活跃的时间 unix 时间戳

	IsSuccessUpgraded bool
	Upgrader          ws.Upgrader
}

协议升级, conn处理 业务层conn

func NewDefaultUpgrader

func NewDefaultUpgrader(conn gnet.Conn) *GnetUpgraderConn

默认的协议升级类

func NewEmptyUpgrader

func NewEmptyUpgrader(conn gnet.Conn) *GnetUpgraderConn

func (GnetUpgraderConn) Read

func (u GnetUpgraderConn) Read(b []byte) (n int, err error)

读数据 这里为什么没用 *GnetUpgraderConn ?

func (*GnetUpgraderConn) UpdateActiveTsToNow

func (u *GnetUpgraderConn) UpdateActiveTsToNow()

更新连接活跃时间到当前时间

func (GnetUpgraderConn) Write

func (u GnetUpgraderConn) Write(b []byte) (n int, err error)

写数据 这里为什么没用 *GnetUpgraderConn ?

type Job

type Job func(param *jobParam)

type Task

type Task struct {
	// contains filtered or unexported fields
}

Task 延时任务

type TimeWheel

type TimeWheel struct {
	// contains filtered or unexported fields
}

TimeWheel 时间轮

func (*TimeWheel) AddTimer

func (tw *TimeWheel) AddTimer(delay time.Duration, key interface{}, data *jobParam)

AddTimer 添加定时器 key为定时器唯一标识

func (*TimeWheel) RemoveTimer

func (tw *TimeWheel) RemoveTimer(key interface{})

RemoveTimer 删除定时器 key为添加定时器时传递的定时器唯一标识

func (*TimeWheel) Start

func (tw *TimeWheel) Start()

Start 启动时间轮

func (*TimeWheel) Stop

func (tw *TimeWheel) Stop()

Stop 停止时间轮

type WebSocketServer

type WebSocketServer struct {
	Addr    string //ip:port
	ConnNum int64

	Handler    DataHandler //业务处理具体实现
	WorkerPool *ants.Pool  //业务处理协程池

	ConnCloseHandler ConnCloseHandleFunc //连接关闭处理
	// contains filtered or unexported fields
}

func NewEchoServer

func NewEchoServer(addr string) *WebSocketServer

func NewServer

func NewServer(addr string) *WebSocketServer

func (*WebSocketServer) OnBoot

func (server *WebSocketServer) OnBoot(eng gnet.Engine) (action gnet.Action)

func (*WebSocketServer) OnClose

func (server *WebSocketServer) OnClose(c gnet.Conn, err error) (action gnet.Action)

func (*WebSocketServer) OnOpen

func (server *WebSocketServer) OnOpen(c gnet.Conn) (out []byte, action gnet.Action)

func (*WebSocketServer) OnShutdown

func (server *WebSocketServer) OnShutdown(eng gnet.Engine)

func (*WebSocketServer) OnTick

func (server *WebSocketServer) OnTick() (delay time.Duration, action gnet.Action)

func (*WebSocketServer) OnTraffic

func (server *WebSocketServer) OnTraffic(c gnet.Conn) (action gnet.Action)

func (*WebSocketServer) React

func (server *WebSocketServer) React(frame []byte, c gnet.Conn) (out []byte, action gnet.Action)

func (*WebSocketServer) SendDownStreamMsg

func (server *WebSocketServer) SendDownStreamMsg(wsConn *GnetUpgraderConn, opcode ws.OpCode, msg []byte) error

发送下行消息

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL