github.com/godevsig/adaptiveservice@v0.9.23/examples/echo/server/server.go (about)

     1  package server
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  
     7  	as "github.com/godevsig/adaptiveservice"
     8  )
     9  
    10  type statMgr struct {
    11  	sync.RWMutex
    12  	clients     map[string]struct{}
    13  	subscribers map[chan string]struct{}
    14  	sessionNum  int32
    15  	counter     int64
    16  }
    17  
    18  const (
    19  	// Publisher is the service(s) publisher
    20  	Publisher = "example"
    21  	// ServiceEcho is the echo service
    22  	ServiceEcho = "echo.v1.0"
    23  )
    24  
    25  // Run runs the server.
    26  func Run(opts []as.Option) {
    27  	s := as.NewServer(opts...).SetPublisher(Publisher)
    28  
    29  	mgr := &statMgr{
    30  		clients:     make(map[string]struct{}),
    31  		subscribers: make(map[chan string]struct{}),
    32  	}
    33  	if err := s.Publish(ServiceEcho,
    34  		echoKnownMsgs,
    35  		as.OnNewStreamFunc(mgr.onNewStream),
    36  		as.OnConnectFunc(mgr.onConnect),
    37  		as.OnDisconnectFunc(mgr.onDisconnect),
    38  	); err != nil {
    39  		fmt.Println(err)
    40  		return
    41  	}
    42  
    43  	if err := s.Serve(); err != nil { // ctrl+c to exit
    44  		fmt.Println(err)
    45  	}
    46  	fmt.Printf("echo server has served %d requests\n", mgr.counter)
    47  }