Example #1
0
/*
The socketHandler for the websocket connection.
Accepts the websocket, hands it off through the socketChan, and
waits until the socket is closed before exiting the function.
//TODO: Actually manage disconnections properly.
*/
func webSocketHandler(ws *websocket.Conn) {
	//Notify the irc manager of a new websocket
	log.Println("socketHandler starting")
	defer log.Println("socketHandler exiting")
	var client ircClient = newWSClient(ws)

	//Authenticate websocket:
	var user *iwcUser = nil

	for user == nil {
		client.SendMessage(irc.NewMessage("Enter a username."))
		msg, err := client.ReceiveMessage()
		if err != nil {
			return
		}
		username := strings.TrimSpace(msg.String())

		client.SendMessage(irc.NewMessage("Enter a password."))
		msg, err = client.ReceiveMessage()
		if err != nil {
			return
		}
		password := strings.TrimSpace(msg.String())
		user = authenticate(username, password)

		if user != nil {
			client.SendMessage("Successfully logged in.")
		} else {
			client.SendMessage(irc.NewMessage("Invalid username/password."))
		}
	}

	newclients := getSessionNotifier(user.username)
	if newclients == nil {
		client.SendMessage("Unable to find session. Closing...")
		log.Printf("Unable to find session for ", user.username)
		return
	}

	//Notify the client what the user's current nick is
	client.SendMessage(irc.NewMessage("NICK " + user.profile.nick.name))
	newclients <- &client

	for {
		if ws.IsServerConn() {
			time.Sleep(100 * time.Millisecond)
		} else {
			log.Println("!!!!socketHandler returning after IsServerConn returned false")
			return
		}
	}
}