Bläddra i källkod

update ws_server interface && others

joe 4 år sedan
förälder
incheckning
3ff42bdae3
4 ändrade filer med 19 tillägg och 18 borttagningar
  1. 2 3
      internal/hub.go
  2. 7 15
      internal/server_ws.go
  3. 1 0
      isession.go
  4. 9 0
      ver.go

+ 2 - 3
internal/hub.go

@@ -105,7 +105,7 @@ var (
 	r = rand.New(s)
 )
 
-func Intn(a int) int {
+func intn(a int) int {
 	return r.Intn(a)
 }
 
@@ -115,7 +115,7 @@ func (self *Hub) RandSession() (nnet.ISession, error) {
 
 	sz := len(self.sess)
 
-	sel := Intn(sz)
+	sel := intn(sz)
 	counter := 0
 	for _, ses := range self.sess {
 		if counter == sel {
@@ -131,4 +131,3 @@ func (self *Hub) GetSessionNum() int {
 	defer self.Unlock()
 	return len(self.sess)
 }
-

+ 7 - 15
internal/server_ws.go

@@ -8,31 +8,23 @@ import (
 	"one.com/nnet"
 )
 
-var (
-//	upgrader = websocket.Upgrader{
-//		ReadBufferSize:  4096,
-//		WriteBufferSize: 4096,
-//		CheckOrigin: func(r *http.Request) bool {
-//			return true
-//		},
-//	}
-)
-
 type CallbackWsPath func(http.ResponseWriter, *http.Request)
 
 type WsServer struct {
 	*Hub
 	addr     string
+	wspath   string
 	svr      *http.Server
 	upgrader *websocket.Upgrader
 	routes   map[string]CallbackWsPath
 }
 
 func NewWsServer(cf *nnet.HubConfig, cb nnet.ISessionCallback, p nnet.IProtocol,
-	addr string, routes map[string]CallbackWsPath) *WsServer {
+	addr string, wspath string, routes map[string]CallbackWsPath) *WsServer {
 	return &WsServer{
-		Hub:  newHub(cf, cb, p),
-		addr: addr,
+		Hub:    newHub(cf, cb, p),
+		addr:   addr,
+		wspath: wspath,
 		upgrader: &websocket.Upgrader{
 			ReadBufferSize:  cf.ReadBufSize,
 			WriteBufferSize: cf.WriteBufSize,
@@ -50,7 +42,7 @@ func (self *WsServer) Start() error {
 		router.HandleFunc(k, v)
 	}
 	router.HandleFunc("/", self.do_homepage)
-	router.HandleFunc("/kg", func(w http.ResponseWriter, r *http.Request) {
+	router.HandleFunc(self.wspath, func(w http.ResponseWriter, r *http.Request) {
 		self.do_new_session(w, r)
 	})
 	self.svr = &http.Server{
@@ -70,7 +62,7 @@ func (self *WsServer) do_homepage(w http.ResponseWriter, r *http.Request) {
 		http.Error(w, "Method now allowed", http.StatusMethodNotAllowed)
 		return
 	}
-	w.Write([]byte("welcome"))
+	w.Write([]byte("welcome to nnet v" + nnet.VERSION))
 }
 
 func (self *WsServer) do_new_session(w http.ResponseWriter, r *http.Request) {

+ 1 - 0
isession.go

@@ -5,6 +5,7 @@ import "time"
 var (
 	DEF_SESSION_ID     uint64 = 0
 	RUBBISH_SESSION_ID uint64 = 1
+	SESSION_ID_START   uint64 = 100
 )
 
 type ISession interface {

+ 9 - 0
ver.go

@@ -0,0 +1,9 @@
+package nnet
+
+const (
+	VERSION = "0.0.1"
+)
+
+func Version () string {
+	return VERSION
+}