예제 #1
0
파일: main.go 프로젝트: pschlump/socketio
func main() {

	junk, err := flags.ParseArgs(&opts, os.Args)

	if len(junk) != 1 {
		fmt.Printf("Usage: Invalid arguments supplied, %s\n", junk)
		os.Exit(1)
	}
	if err != nil {
		os.Exit(1)
	}

	port = fmt.Sprintf("%d", opts.Port)
	if opts.HostIP != "localhost" {
		host_ip = opts.HostIP
	}

	server, err := socketio.NewServer(nil)
	if err != nil {
		log.Fatal(err)
	}
	server.On("connection", func(so socketio.Socket) {
		log.Println("on connection")
		so.Join("chat")
		so.On("chat message", func(msg string) {
			m := make(map[string]interface{})
			m["a"] = "你好" // hello there
			e := so.Emit("cn1111", m)
			//这个没有问题			// this is no problem
			fmt.Println("\n\n")

			b := make(map[string]string)
			b["u-a"] = "中文内容" //这个不能是中文		// this is chineese // this can not be chineese
			m["b-c"] = b
			e = so.Emit("cn2222", m)
			log.Println(e)

			log.Println("emit:", so.Emit("chat message", msg))
			so.BroadcastTo("chat", "chat message", msg)
		})
		so.On("t45", func(msg string) {
			err := so.Emit("r45", "Yep")
			if err != nil {
				fmt.Printf("Error: %s\n", err)
			}
		})
		so.On("disconnection", func() {
			log.Println("on disconnect")
		})
	})
	server.On("error", func(so socketio.Socket, err error) {
		log.Println("error:", err)
	})

	http.Handle("/socket.io/", server)
	http.Handle("/", http.FileServer(http.Dir("./asset")))
	fmt.Printf("Serving on port %s, brows to http://localhost:%s/\n", port, port)
	listen := fmt.Sprintf("%s:%s", host_ip, port)
	log.Fatal(http.ListenAndServe(listen, nil))
}
예제 #2
0
파일: main.go 프로젝트: pschlump/socketio
func main() {

	flag.Parse()
	fns := flag.Args()

	if len(fns) != 0 {
		fmt.Printf("Usage: Invalid arguments supplied, %s\n", fns)
		os.Exit(1)
	}

	var host_ip string = ""
	if *HostIP != "localhost" {
		host_ip = *HostIP
	}

	// Make certain that the command line parameters are handled correctly
	// fmt.Printf("host_ip >%s< HostIP >%s< Port >%s<\n", host_ip, *HostIP, *Port)

	server, err := socketio.NewServer(nil)
	if err != nil {
		log.Fatal(err)
	}

	server.On("connection", func(so socketio.Socket) {
		fmt.Printf("%sa user connected%s, %s\n", MiscLib.ColorGreen, MiscLib.ColorReset, godebug.LF())
		so.Join("chat")
		so.On("chat message", func(msg string) {
			fmt.Printf("%schat message, %s%s, %s\n", MiscLib.ColorGreen, msg, MiscLib.ColorReset, godebug.LF())
			so.BroadcastTo("chat", "chat message", msg)
		})
		so.On("disconnect", func() {
			fmt.Printf("%suser disconnect%s, %s\n", MiscLib.ColorYellow, MiscLib.ColorReset, godebug.LF())
		})
	})

	server.On("error", func(so socketio.Socket, err error) {
		fmt.Printf("Error: %s, %s\n", err, godebug.LF())
	})

	http.Handle("/socket.io/", server)
	http.Handle("/", http.FileServer(http.Dir(*Dir)))
	fmt.Printf("Serving on port %s, browse to http://localhost:%s/\n", *Port, *Port)
	listen := fmt.Sprintf("%s:%s", host_ip, *Port)
	log.Fatal(http.ListenAndServe(listen, nil))
}
예제 #3
0
파일: main.go 프로젝트: pschlump/socketio
func main() {

	junk, err := flags.ParseArgs(&opts, os.Args)

	if len(junk) != 1 {
		fmt.Printf("Usage: Invalid arguments supplied, %s\n", junk)
		os.Exit(1)
	}
	if err != nil {
		os.Exit(1)
	}

	port = fmt.Sprintf("%d", opts.Port)
	if opts.HostIP != "localhost" {
		host_ip = opts.HostIP
	}

	server, err := socketio.NewServer(nil)
	if err != nil {
		log.Fatal(err)
	}
	server.On("connection", func(so socketio.Socket) {
		log.Println("on connection")
		so.Join("chat")
		so.On("chat message", func(msg string) {
			m := make(map[string]interface{})
			m["a"] = "你好" // hello there
			e := so.Emit("cn1111", m)
			//这个没有问题			// this is no problem
			fmt.Println("\n\n")

			b := make(map[string]string)
			b["u-a"] = "中文内容" //这个不能是中文		// this is chineese // this can not be chineese
			m["b-c"] = b
			e = so.Emit("cn2222", m)
			log.Println(e)

			log.Println("emit:", so.Emit("chat message", msg))
			so.BroadcastTo("chat", "chat message", msg)
		})
		so.On("t45", func(msg string) {
			err := so.Emit("r45", "Yep")
			if err != nil {
				fmt.Printf("Error: %s\n", err)
			}
		})
		so.On("registerForName", func(name, pass string) {
			b := ValidateUser(name, pass)
			// store the soketio.Socket in a global map
			UserMapLock.Lock()
			UserMap[name] = &UserInfo{
				User:       name,
				IsLoggedIn: b,
				So:         so,
			}
			UserMapLock.Unlock()
			if b {
				so.Emit("chat message", "logged in")
			} else {
				so.Emit("chat message", "invalid username/password (try bob/123 or jane/abc)")
			}
		})
		so.On("logout", func(name string) {
			UserMapLock.Lock()
			delete(UserMap, name)
			UserMapLock.Unlock()
			so.Emit("chat message", "bye now")
		})
		so.On("sendMessageTo", func(iAm, name string, message string) {
			fmt.Printf("sendMessage func, name = %v, message = %v", name, message)
			UserMapLock.RLock()
			defer UserMapLock.RUnlock()
			from, ok1 := UserMap[iAm]
			if !ok1 || !from.IsLoggedIn {
				so.Emit("error", "Error: you are not logged in.")
				return
			}
			dest, ok2 := UserMap[name]
			if !ok2 || !dest.IsLoggedIn {
				so.Emit("error", "Error: not logged in:"+name)
				return
			}
			dest.So.Emit("chat message", message)
		})
		so.On("disconnection", func() {
			// xyzzy - get "name" from so.Id() - use for auto/logout on disconnect - cleanup of global hash
			log.Println("on disconnect")
		})
	})
	server.On("error", func(so socketio.Socket, err error) {
		log.Println("error:", err)
	})

	// ------------------ Send periodic time notices to client -------------------------------------------------------------------
	ticker := time.NewTicker(10 * time.Second)
	quit := make(chan struct{})
	go func() {
		for {
			select {
			case <-ticker.C:
				// do stuff
				server.BroadcastTo("chat", "chat message", "I am alive")
				if u, ok := UserMap["bob"]; ok {
					u.So.Emit("chat message", "Hi Bob!")
				}
			case <-quit:
				ticker.Stop()
				return
			}
		}
	}()

	http.Handle("/socket.io/", server)
	http.Handle("/", http.FileServer(http.Dir("./asset")))
	fmt.Printf("Serving on port %s, brows to http://localhost:%s/\n", port, port)
	listen := fmt.Sprintf("%s:%s", host_ip, port)
	log.Fatal(http.ListenAndServe(listen, nil))
}