kataras / neffos

A modern, fast and scalable websocket framework with elegant API written in Go
http://bit.ly/neffos-wiki
MIT License
572 stars 47 forks source link

[BUG] client how do reconnect? #77

Open aadog opened 1 year ago

aadog commented 1 year ago

client how do reconnect?

package main

import (
    "context"
    "fmt"
    "github.com/kataras/iris/v12/websocket"
    "github.com/kataras/neffos"
    "log"
    "time"
)

const (
    endpoint              = "ws://192.168.2.45:7000/join"
    namespace             = "default"
    dialAndConnectTimeout = 6 * time.Second
)

var clientEvents = websocket.Namespaces{
    namespace: websocket.Events{
        websocket.OnNamespaceConnected: func(c *websocket.NSConn, msg websocket.Message) error {
            log.Printf("connected to namespace: %s", msg.Namespace)

            room, err := c.JoinRoom(context.TODO(), "default")
            fmt.Println(err)
            fmt.Println(room.String())
            return nil
        },
        websocket.OnNamespaceDisconnect: func(c *websocket.NSConn, msg websocket.Message) error {
            log.Printf("disconnected from namespace: %s", msg.Namespace)
            fmt.Println("reconnect")
            time.Sleep(time.Second * 1)
            fmt.Println(c.Conn)
            return nil
        },
        websocket.OnAnyEvent: func(conn *neffos.NSConn, message neffos.Message) error {
            fmt.Println(message)
            return nil
        },
        websocket.OnNativeMessage: func(conn *neffos.NSConn, message neffos.Message) error {
            fmt.Println(message)
            return nil
        },
        "chat": func(c *websocket.NSConn, msg websocket.Message) error {
            log.Printf("%s", string(msg.Body))
            return nil
        },
    },
}

// 启动
// 检查py引擎是否启动并且上报状态
// 停止
// 保持链接
// 是否运行脚本(当前运行脚本)
// 查看日志
// 更新py 工程
func main() {
    //cmd := exec.Command("whoami")
    //cmd.Stdout = os.Stdout
    //cmd.Stderr = os.Stderr
    //cmd.Run()
    ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(dialAndConnectTimeout))
    defer cancel()
    dialer := websocket.DefaultGobwasDialer
    client, err := websocket.Dial(ctx, dialer, endpoint, clientEvents)
    if err != nil {
        panic(err)
    }
    defer client.Close()
    client.Connect(ctx, namespace)
    fmt.Println("wait conn")
    for {
        time.Sleep(time.Second * 1)
    }
}