예제 #1
0
func unregisterClient(ws *websocket.Conn) {
	var n string

	// Remove client
	for k, v := range clients {
		if v.Conn == ws {
			n = k
			delete(clients, k)
		}
	}

	s := false
	if len(clients) == 1 {
		s = true
	}

	// Notify
	msg := lib.Message{
		Kind: "leave",
		Body: n,
		Args: lib.MakeArgs(s),
	}

	sendToOthers(ws, msg)
}
예제 #2
0
func registerClient(ws *websocket.Conn) {

	n := len(clients)
	u := strconv.Itoa(n)
	if n < 10 {
		u = "0" + u
	}

	// Store client in clients map
	clients[u] = lib.Client{
		Id:   u,
		Name: defaultName + u,
		Conn: ws,
	}

	// Send welcome message
	var msg lib.Message

	msg = lib.Message{
		Kind: "info",
		Body: lib.AppendString("[", lib.PrintTimeStamp(), "] ", "Welcome, ", clients[u].Name),
		Args: lib.MakeArgs(clients[u].Name),
	}

	if err := sendToClient(ws, msg); err != nil {
		debug.Printf("Error sending message: %s\n", err)
	}

	msg = lib.Message{
		Kind: "info",
		Body: lib.AppendString("[", lib.PrintTimeStamp(), "] ", clients[u].Name, " joined"),
	}

	if err := sendToOthers(ws, msg); err != nil {
		debug.Printf("Error sending mesaage to others: %s\n", err)
	}
}
예제 #3
0
func wsHandler(ws *websocket.Conn) {
	registerClient(ws)

	for {
		var msg lib.Message
		var out lib.Message

		if err := websocket.JSON.Receive(ws, &msg); err != nil {
			debug.Printf("Error reading message: %s\n", err)
			unregisterClient(ws)
			return
		}

		debug.Printf("Received message: %s\n", msg.Kind)

		switch msg.Kind {
		case "format":
			data, err := lib.Format([]byte(msg.Body))
			if err != nil {
				debug.Printf("Format Error: %s\n", err)

				out = lib.Message{
					Kind: "error",
					Body: err.Error(),
				}

				if err := sendToAll(ws, out); err != nil {
					debug.Printf("Error sending message: %s\n", err)
				}

			}

			if c := getClient(ws); c != nil {
				out = lib.Message{
					Kind: "code",
					Body: string(data),
					Args: lib.MakeArgs(c.Name),
				}
				if err := sendToAll(ws, out); err != nil {
					debug.Printf("Error sending message: %s\n", err)
				}
			}

		case "save":
			data, err := lib.CreateGist("GoGist", msg.Body)
			if err != nil {
				debug.Printf("Error creating gist: %v\n", err)
			}

			resp, err := lib.ParseResponse(data)
			if err != nil {
				debug.Printf("Error parsing Gists response: %s\n", err)
			}

			s := fmt.Sprintf("%s", resp["html_url"])

			out = lib.Message{
				Kind: "gist",
				Body: s,
			}

			if err := sendToAll(ws, out); err != nil {
				debug.Printf("Error sending message: %s\n", err)
			}

		case "compile":
			data, err := lib.Compile(msg.Body)
			if err != nil {
				debug.Printf("Error compiling code (remote): %s\n", err)
			}

			cr, err := lib.ParseCompileResponse(data)
			if err != nil {
				debug.Printf("Error parsing compile response: %s\n", err)
			}

			// WTF!
			if s := cr.Message(); s != nil {
				if ok := s.(string); ok != "" {
					out = lib.Message{
						Kind: "stdout",
						Body: s.(string),
					}

					sendToAll(ws, out)
				}
			}

		case "chat":
			t := time.Now().Format(time.Kitchen)

			if c := getClient(ws); c != nil {
				out = lib.Message{
					Kind: "chat",
					Body: lib.AppendString("[", t, "]", c.Name, ": ", msg.Body),
				}
				sendToAll(ws, out)
			}

		case "update":
			if c := getClient(ws); c != nil {
				out = lib.Message{
					Kind: "update",
					Body: msg.Body,
					Args: lib.MakeArgs(c.Name),
				}

				if err := sendToOthers(ws, out); err != nil {
					debug.Printf("Error sending message: %s\n", err)
				}
			}
		}

	}
}