Skip to content
GaoShou012Public

About

No description, website, or topics provided.

Resources

Stars

2 stars

Watchers

1 watching

Forks

Repository files navigation

简介

The frontier is a server of Websocket

例子

package main

import (
	"fmt"
	"github.com/GaoShou012/frontier"
	"github.com/GaoShou012/tools/logger"
	"github.com/golang/glog"
	uuid "github.com/satori/go.uuid"
	"os"
	"os/signal"
	"syscall"
	"time"
)

func main() {
    id := uuid.NewV4().String()
	addr := ":1234"
	maxConnections := 1000000
	dynamicParams := &frontier.DynamicParams{
		LogLevel:         logger.LogAll,
		HeartbeatTimeout: 90,
		WriterBufferSize: 1024 * 4,
		ReaderBufferSize: 1024 * 4,
		WriterTimeout:    time.Millisecond * 40,
		ReaderTimeout:    time.Microsecond * 10,
	}
	handler := &frontier.Handler{
		OnRequest:       nil,
		OnHost:          nil,
		OnHeader:        nil,
		OnBeforeUpgrade: nil,
		OnOpen: func(conn frontier.Conn) error {
			return nil
		},
		OnMessage: func(conn frontier.Conn, message []byte) {
			messageCounter++
			fmt.Println(message)
		},
		OnClose: func(conn frontier.Conn) {
		},
	}

	f := &frontier.Frontier{
		Id:             id,
		Addr:           addr,
		MaxConnections: maxConnections,
		DynamicParams:  dynamicParams,
		Protocol:       &frontier.ProtocolWs{},
		Handler:        handler,
	}
	if err := f.Init(); err != nil {
		panic(err)
	}
	if err := f.Start(); err != nil {
		panic(err)
	}

	c := make(chan os.Signal, 1)
	signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
	for {
		switch s := <-c; s {
		case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
			glog.Infof("got signal %s; stop server", s)
		case syscall.SIGHUP:
			glog.Infof("got signal %s; go to deamon", s)
			continue
		}
		if err := f.Stop(); err != nil {
			glog.Errorf("stop server error: %v", err)
		}
		break
	}
}

About

No description, website, or topics provided.

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages

⌂ 换网址