Documentation ¶
Index ¶
- Constants
- Variables
- func EchoDataHandler(param *DataHandlerParam)
- type ConnCloseHandleFunc
- type DataHandler
- type DataHandlerParam
- type GnetUpgraderConn
- type Job
- type Task
- type TimeWheel
- type WebSocketServer
- func (server *WebSocketServer) OnBoot(eng gnet.Engine) (action gnet.Action)
- func (server *WebSocketServer) OnClose(c gnet.Conn, err error) (action gnet.Action)
- func (server *WebSocketServer) OnOpen(c gnet.Conn) (out []byte, action gnet.Action)
- func (server *WebSocketServer) OnShutdown(eng gnet.Engine)
- func (server *WebSocketServer) OnTick() (delay time.Duration, action gnet.Action)
- func (server *WebSocketServer) OnTraffic(c gnet.Conn) (action gnet.Action)
- func (server *WebSocketServer) React(frame []byte, c gnet.Conn) (out []byte, action gnet.Action)
- func (server *WebSocketServer) SendDownStreamMsg(wsConn *GnetUpgraderConn, opcode ws.OpCode, msg []byte) error
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 ¶
Types ¶
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 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()
更新连接活跃时间到当前时间
type TimeWheel ¶
type TimeWheel struct {
// contains filtered or unexported fields
}
TimeWheel 时间轮
func (*TimeWheel) RemoveTimer ¶
func (tw *TimeWheel) RemoveTimer(key interface{})
RemoveTimer 删除定时器 key为添加定时器时传递的定时器唯一标识
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
发送下行消息
Click to show internal directories.
Click to hide internal directories.