Ejemplo n.º 1
0
func main() {
	sio := socketio.NewSocketIO(nil)

	sio.OnConnect(func(c *socketio.Conn) {
		sio.Broadcast(struct{ announcement string }{"connected: " + c.String()})
	})

	sio.OnDisconnect(func(c *socketio.Conn) {
		sio.BroadcastExcept(c,
			struct{ announcement string }{"disconnected: " + c.String()})
	})

	sio.OnMessage(func(conn *socketio.Conn, msg socketio.Message) {
		// var col teapot.Collation

		if msg.Type() == socketio.MessageText {
			name := msg.Data()
			switch name {
			case "getToken":
				token, _ := genToken()
				conn.Send(token)
			case "annotateCollation":
				//var key, value string
				//msg.ReadArguments(&key, &value)
				//col.Annotate(key, value)
				conn.Send("collationAnnotated")
			case "addMedia":
				// Is it possible to get a file stream through js event message??
				// probably not - so will have to implement form upload and messaging.
				conn.Send("mediaAdded")
			case "annotateMedia":
				// var media, key, value string
				// msg.ReadArguments(&media, &key, &value)
				// mfile := c.GetMediaByKey(media)
				// mfile.Annotate(key, value)
				conn.Send("mediaAnnotated")
			}
		}
	})

	htmlDir, _ := os.Getwd()
	mux := sio.ServeMux()
	mux.Handle("/html/", http.FileServer(http.Dir(htmlDir)))
	mux.HandleFunc("/media", mediaHandler)

	if err := http.ListenAndServe(":80", mux); err != nil {
		log.Fatal("ListenAndServe:", err)
	}

	/* Create a goroutine to watch the beanstalk queue for messages */
}
Ejemplo n.º 2
0
// A very simple chat server
func main() {
	var buffer []interface{}
	mutex := new(sync.Mutex)

	// create the socket.io server and mux it to /socket.io/
	config := socketio.DefaultConfig
	config.Origins = []string{"localhost:8080"}
	sio := socketio.NewSocketIO(&config)

	go func() {
		if err := sio.ListenAndServeFlashPolicy(":843"); err != nil {
			log.Println(err)
		}
	}()

	// when a client connects - send it the buffer and broadcasta an announcement
	sio.OnConnect(func(c *socketio.Conn) {
		mutex.Lock()
		b := make([]interface{}, len(buffer))
		copy(b, buffer)
		c.Send(Buffer{b})
		mutex.Unlock()
		sio.Broadcast(Announcement{"connected: " + c.String()})
	})

	// when a client disconnects - send an announcement
	sio.OnDisconnect(func(c *socketio.Conn) {
		sio.Broadcast(Announcement{"disconnected: " + c.String()})
	})

	// when a client send a message - broadcast and store it
	sio.OnMessage(func(c *socketio.Conn, msg socketio.Message) {
		payload := Message{[]string{c.String(), msg.Data()}}
		mutex.Lock()
		buffer = append(buffer, payload)
		mutex.Unlock()
		sio.Broadcast(payload)
	})

	log.Println("Server starting. Tune your browser to http://localhost:8080/")

	mux := sio.ServeMux()
	mux.Handle("/", http.FileServer(http.Dir("www/")))

	if err := http.ListenAndServe(":8080", mux); err != nil {
		log.Fatal("ListenAndServe:", err)
	}
}
Ejemplo n.º 3
0
func NewGame(port string) (g *Game) {
	config := socketio.DefaultConfig
	config.Logger = nil
	config.Origins = []string{"www.projectgeky.com:" + port}

	g = &Game{
		false,
		make(map[socketio.SessionID]*Player),
		[]uint32{}, 0, 0, 0, nil,
		socketio.NewSocketIO(&config),
		new(sync.Mutex),
	}

	g.SIO.OnDisconnect(func(c *socketio.Conn) {
		g.Disconnect(c.SessionID())
	})

	g.SIO.OnMessage(func(c *socketio.Conn, m socketio.Message) {
		//log.Println("!"+m.Data()+"!")
		var msg map[string]interface{}
		if json.Unmarshal(m.Bytes(), &msg) != nil {
			return
		}

		for key, data := range msg {
			switch key {
			case "gamer", "watcher":
				v, ok := data.(map[string]interface{})
				if !ok {
					return
				}
				name, ok := v["name"].(string)
				if !ok {
					return
				}
				color, ok := v["color"].(float64)
				if !ok {
					return
				}

				p := &Player{name, int(color), c.SessionID(), key == "gamer"}

				g.Mutex.Lock()
				g.Players[p.ID] = p
				g.SIO.BroadcastExcept(c, struct{ Join *Player }{p})

				type init struct {
					ID      socketio.SessionID             `json:"id"`
					Players map[socketio.SessionID]*Player `json:"players"`
				}
				c.Send(struct{ Init init }{init{c.SessionID(), g.Players}})
				g.Mutex.Unlock()

				log.Println(p.String() + " has joined")

			case "chat":
				v, ok := data.(string)
				if !ok || len(v) > 512 {
					return
				}

				type message struct {
					ID      socketio.SessionID `json:"id"`
					Message string             `json:"msg"`
				}
				g.SIO.Broadcast(struct{ Chat message }{message{c.SessionID(), v}})

			case "change":
				v, ok := data.(bool)
				if !ok {
					return
				}

				g.Mutex.Lock()
				p := g.Players[c.SessionID()]
				if p == nil {
					g.Mutex.Unlock()
					return
				}

				p.Game = v
				g.Mutex.Unlock()

				type change struct {
					ID   socketio.SessionID `json:"id"`
					Game bool               `json:"game"`
				}
				g.SIO.Broadcast(struct{ Change change }{change{c.SessionID(), v}})

			case "disc":
				g.Disconnect(c.SessionID())

			case "move":
				v, ok := data.(map[string]interface{})
				if !ok {
					return
				}
				posx, ok := v["posx"].(float64)
				if !ok {
					return
				}
				posy, ok := v["posy"].(float64)
				if !ok {
					return
				}

				p := &Gamer{posx, posy}

				g.Mutex.Lock()
				g.Gamers[c.SessionID()] = p
				g.Mutex.Unlock()
			}
		}
	})
	return
}