Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] client how do reconnect? #77

Open
aadog opened this issue Jan 3, 2023 · 0 comments
Open

[BUG] client how do reconnect? #77

aadog opened this issue Jan 3, 2023 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@aadog
Copy link

aadog commented Jan 3, 2023

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)
	}
}

@aadog aadog added the bug Something isn't working label Jan 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants