Commit 443d96e6 authored by ibuler's avatar ibuler

[Update] 修改支持心跳

parent 096757c8
......@@ -11,10 +11,16 @@
var scheme = document.location.protocol == "https:" ? "wss" : "ws";
var port = document.location.port ? ":" + document.location.port : "";
var wsURL = scheme + "://" + document.location.hostname + port + "/socket.io/";
let interval;
dial(wsURL, {
"elfinder": {
_OnNamespaceConnected: function (nsConn, msg) {
console.log("Connect websocket done")
interval = setInterval(() => nsConn.emit('ping', ''), 10000);
},
_OnNamespaceDisconnect: function (ns, msg) {
if (interval) {
clearInterval(interval);
}
},
data: function (nsConn, msg) {
var data = msg.unmarshal();
......
......@@ -16,11 +16,12 @@ import (
var (
httpServer *http.Server
Timeout = time.Duration(60)
)
var wsEvents = neffos.WithTimeout{
ReadTimeout: 24 * time.Hour,
WriteTimeout: 24 * time.Hour,
ReadTimeout: Timeout * time.Second,
WriteTimeout: Timeout * time.Second,
Namespaces: neffos.Namespaces{
"ssh": neffos.Events{
neffos.OnNamespaceConnected: OnNamespaceConnected,
......@@ -37,10 +38,12 @@ var wsEvents = neffos.WithTimeout{
"host": OnHostHandler,
"logout": OnLogoutHandler,
"token": OnTokenHandler,
"ping": OnPingHandler,
},
"elfinder": neffos.Events{
neffos.OnNamespaceConnected: OnELFinderConnect,
neffos.OnNamespaceDisconnect: OnELFinderDisconnect,
"ping": OnPingHandler,
},
},
}
......@@ -55,10 +58,11 @@ func StartHTTPServer() {
}
sshWs.OnConnect = func(c *neffos.Conn) error {
if c.WasReconnected() {
logger.Debugf("Connection reconnected, with tries: %d", c.ID(), c.ReconnectTries)
logger.Debugf("Connection %s reconnected, with tries: %d", c.ID(), c.ReconnectTries)
} else {
logger.Debug("A new ws connection arrive")
}
return nil
}
sshWs.OnDisconnect = func(c *neffos.Conn) {
......
......@@ -9,6 +9,7 @@ import (
"net"
"strings"
"sync"
"time"
"github.com/gliderlabs/ssh"
"github.com/kataras/neffos"
......@@ -20,14 +21,16 @@ import (
"github.com/jumpserver/koko/pkg/service"
)
func OnPingHandler(c *neffos.NSConn, msg neffos.Message) error {
c.Emit("pong", []byte(""))
return nil
}
// OnConnectHandler 当websocket连接后触发
func OnNamespaceConnected(c *neffos.NSConn, msg neffos.Message) error {
// 首次连接 1.获取当前用户的信息
cc := c.Conn
logger.Debug("Web terminal on connect event trigger")
if cc.WasReconnected() {
} else {
}
request := cc.Socket().Request()
header := request.Header
cookies := strings.Split(header.Get("Cookie"), ";")
......@@ -53,6 +56,12 @@ func OnNamespaceConnected(c *neffos.NSConn, msg neffos.Message) error {
}
remoteIP = strings.Split(remoteAddr, ",")[0]
logger.Infof("Accepted %s connect websocket from %s", user.Username, remoteIP)
go func() {
for {
<-time.After(30 * time.Second)
c.Emit("ping", []byte(""))
}
}()
return nil
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment