示例#1
0
文件: main.go 项目: xnattack/socket
func main() {
	server := socket.NewServer()

	server.On(socket.Connection, func(so socket.Socket) {
		so.Emit("hello", "world")
		so.On("hey", func(msg string) {
			fmt.Println("WORKING")
			fmt.Println(msg)
		})
	})

	server.Of("/views").On(socket.Connection, func(so socket.Socket) {
		so.Join("group")
		fmt.Println(so.ID(), "joined")
		so.To("group").Emit("hello", so.ID())
		so.On("hey", func(msg string) {
			fmt.Println(so.ID(), msg)
		})
		so.On(socket.Disconnect, func() {
			fmt.Println(so.ID(), "leaving")
		})
	})

	server.Of("/views").On(socket.Disconnection, func(so socket.Socket) {
		fmt.Println(so.ID(), "left")
		so.To("group").Emit("goodbye", so.ID())
	})

	go func() {
		if so, err := socket.New("localhost/views"); err == nil {
			so.Emit("hey", ":)")
			so.On("hello", func(msg string) {
				fmt.Println(msg, ": hello")
			})
		} else {
			fmt.Println(err.Error())
		}
	}()

	router := http.NewServeMux()
	router.Handle("/socket", server)
	router.Handle("/", http.FileServer(http.Dir(".")))
	http.ListenAndServe("localhost:80", router)
}
示例#2
0
文件: client.go 项目: xnattack/socket
func main() {

	so, err := socket.New("localhost")
	if err != nil {
		log.Fatal(err.Error())
	}

	wait := make(chan struct{})

	so.On("hook", func(msg string) {
		fmt.Println(msg)
		wait <- struct{}{}
	})

	so.On(socket.Disconnect, func() {
		log.Println("Server hung-up")
		wait <- struct{}{}
	})

	<-wait
}