Esempio n. 1
0
func main() {
	socks = make(map[*gotalk.Sock]int)
	rooms = make(RoomMap)

	// Load names data
	if namesjson, err := ioutil.ReadFile("names.json"); err != nil {
		panic("failed to read names.json: " + err.Error())
	} else if err := json.Unmarshal(namesjson, &names); err != nil {
		panic("failed to read names.json: " + err.Error())
	}
	rand.Seed(time.Now().UTC().UnixNano())

	// Add some sample rooms and messages
	createRoom("animals").appendMessage(
		&Message{randomName(), "I like cats"})
	createRoom("jokes").appendMessage(
		&Message{randomName(), "Two tomatoes walked across the street ..."})
	createRoom("golang").appendMessage(
		&Message{randomName(), "func(func(func(func())func()))func()"})

	// Register our handlers
	gotalk.Handle("list-messages", func(roomName string) ([]*Message, error) {
		room := findRoom(roomName)
		if room == nil {
			return nil, errors.New("no such room")
		}
		return room.messages, nil
	})

	gotalk.Handle("send-message", func(s *gotalk.Sock, r NewMessage) error {
		if len(r.Message.Body) == 0 {
			return errors.New("empty message")
		}
		username, _ := s.UserData.(string)
		room := findRoom(r.Room)
		room.appendMessage(&Message{username, r.Message.Body})
		r.Message.Author = username
		broadcast("newmsg", &r)
		return nil
	})

	gotalk.Handle("create-room", func(name string) (*Room, error) {
		if len(name) == 0 {
			return nil, errors.New("empty name")
		}
		return createRoom(name), nil
	})

	// Serve gotalk at "/gotalk/"
	gotalkws := gotalk.WebSocketHandler()
	gotalkws.OnAccept = onAccept
	http.Handle("/gotalk/", gotalkws)

	http.Handle("/", http.FileServer(http.Dir(".")))
	err := http.ListenAndServe(":1235", nil)
	if err != nil {
		panic("ListenAndServe: " + err.Error())
	}
}
Esempio n. 2
0
func main() {
	gotalk.Handle("echo", func(in string) (string, error) {
		return in, nil
	})
	http.Handle("/gotalk/", gotalk.WebSocketHandler())
	http.Handle("/", http.FileServer(http.Dir(".")))
	err := http.ListenAndServe("localhost:1234", nil)
	if err != nil {
		panic(err)
	}
}
Esempio n. 3
0
func main() {
	gotalk.Handle("greet", func(in GreetIn) (GreetOut, error) {
		println("in greet handler: in.Name=", in.Name)
		return GreetOut{"Hello " + in.Name}, nil
	})

	gotalk.HandleBufferRequest("echó", func(s *gotalk.Sock, op string, b []byte) ([]byte, error) {
		return b, nil
	})

	ws := gotalk.WebSocketHandler()
	ws.OnAccept = onAccept
	http.Handle("/gotalk/", ws)
	http.Handle("/", http.FileServer(http.Dir(".")))
	err := http.ListenAndServe(":1234", nil)
	if err != nil {
		panic(err)
	}
}
Esempio n. 4
0
func main() {
	database = db.Connect()
	defer database.Close()

	ws := gotalk.WebSocketHandler()
	ws.OnAccept = onAccept

	wwwPath := "./www"
	if envWWWPath := os.Getenv("WWW_PATH"); envWWWPath != "" {
		wwwPath = envWWWPath
	}

	listenAddr := ":7654"
	if envListenAddr := os.Getenv("WWW_PATH"); envListenAddr != "" {
		listenAddr = envListenAddr
	}

	http.Handle("/gotalk/", ws)
	http.Handle("/", http.FileServer(http.Dir(wwwPath)))
	panic(http.ListenAndServe(listenAddr, nil))
}