Beispiel #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())
	}
}
Beispiel #2
0
func main() {
	// Create two connected sockets with default handlers and limits
	s1, s2, err := gotalk.Pipe(nil, nil)
	if err != nil {
		panic(err.Error())
	}

	// Give the sockets names so we can include it in the greetings
	s1.UserData = "socket#1"
	s2.UserData = "socket#2"

	// Handle greetings
	gotalk.Handle("greet", func(s *gotalk.Sock, name string) (string, error) {
		sockname, _ := s.UserData.(string)
		return "Hello " + name + " from " + sockname, nil
	})

	// Send a "greet" request to each socket, making the opposite side respond.
	log.Printf("greet(s1, \"Bob\")  => %+v\n", requestGreet(s1, "Bob"))
	log.Printf("greet(s2, \"Lisa\") => %+v\n", requestGreet(s2, "Lisa"))

	// Output
	//   greet(s1, "Bob")  => {Greeting:Hello Bob from socket#2}
	//   greet(s2, "Lisa") => {Greeting:Hello Lisa from socket#1}

	s1.Close()
	s2.Close()
}
Beispiel #3
0
func init() {
	gotalk.Handle("client.init-google", sessionLoginGoogle)
	gotalk.Handle("client.signout-google", sessionLogoutGoogle)

	if envGoogleClientID := os.Getenv("GOOGLE_CLIENTID"); envGoogleClientID != "" {
		googleClientID = envGoogleClientID
	}

	if len(googleClientID) == 0 {
		panic("GOOGLE_CLIENTID must not be empty.")
	}

	var err error
	oa2, err = oauth2.New(&http.Client{})
	if err != nil {
		panic(err)
	}
}
Beispiel #4
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)
	}
}
Beispiel #5
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)
	}
}
Beispiel #6
0
func init() {
	gotalk.Handle("person.create", create)
}
Beispiel #7
0
func init() {
	gotalk.Handle("person.get", func(s *gotalk.Sock, r *GetRequest) (*Person, error) { fmt.Println(r.ID); return get(s, *r.ID) })
}