Esempio n. 1
0
//OnConnection donne les actions à effectuer lorsque l'on a une connexion
func (server *Server) onConnection(so socketio.Socket) {
	logger.Print("Client connection")

	roomName := constants.DefaultRoom
	user := &user.User{Login: "******", GravatarLink: "", Socket: &so}

	so.Join(roomName)
	logger.Print("Client join " + roomName + " room")

	// Changement et création de salles
	so.On("changeRoom", func(msg string) {
		server.roomChangement(user, msg)
	})

	// Réception de messages
	so.On("message", func(msg string) {
		server.messageReception(user, msg)
	})

	//Login d'un utilisateur
	so.On("login", func(msg string) {
		server.tryLoginUser(user, msg)
	})

	so.On("register", func(msg string) {
		server.tryInscription(user, msg)
	})

	so.On("disconnection", func() {
		oldRoom := user.Room

		if server.roomList.RemoveUserFromRoom(user) {
			so.BroadcastTo(oldRoom, "userLeft", "{\"Login\": \""+user.Login+"\"}")
			so.Leave(oldRoom)
		}
	})
}
Esempio n. 2
0
func main() {
	var port string

	flag.Parse()

	if flag.NArg() == 1 {
		port = ":" + flag.Arg(0)
	} else {
		port = constants.PORT
	}

	server := server.CreateServer()
	logger.Print("Serving at localhost" + port + "...")

	server.Listen(port)
}
Esempio n. 3
0
func (server *Server) changeUserRoom(u *user.User, roomName string) {

	logger.Print("Changement de salle : " + roomName)
	socket := *u.Socket

	oldRoom := u.Room
	if server.roomList.RemoveUserFromRoom(u) {
		socket.BroadcastTo(oldRoom, "userLeft", "{\"Login\": \""+u.Login+"\"}")
		socket.Leave(oldRoom)
	}

	newRoomCreated := false
	if !server.roomList.Exist(roomName) {
		server.roomList.AddRoom(roomName)
		newRoomCreated = true
		rooms := server.roomList.GetRoomsTab()
		for _, value := range rooms {
			err := socket.BroadcastTo(value, "newRoom", "{\"Name\":\""+roomName+"\"}")
			if err != nil {
				logger.Error("newRoom - ", err)
			}
		}
	}

	success := false
	if server.roomList.AddUserInRoom(u, roomName) == nil {

		socket.Join(roomName)
		socket.BroadcastTo(roomName, "newUser", "{\"Login\": \""+u.Login+"\",\"GravatarLink\": \""+u.GravatarLink+"\"}")

		success = true
	}

	var reply room.ChangeRoomReply
	newRoom := server.roomList.GetRoom(roomName)
	if newRoom != nil {
		reply = room.ChangeRoomReply{success, roomName, newRoomCreated, newRoom.GetUsersDetails(), newRoom.GetMessages()}
	} else {
		reply = room.ChangeRoomReply{success, roomName, newRoomCreated, nil, nil}
	}

	time.Sleep(500 * time.Millisecond)
	socket.Emit("changeRoom", reply.ToString())
}
Esempio n. 4
0
// tryLoginUser try to login user
func (server *Server) tryLoginUser(u *user.User, message string) {

	socket := *u.Socket

	request := user.GetLoginRequest(message)
	login, password, newUser := user.ConnectSite(request.Login, request.Password)

	success := login && password

	reply := user.LoginReply{success, login, password, request.Login, server.roomList.GetRoomsTab(), u.GravatarLink}
	socket.Emit("login", reply.String())

	if success {
		u.Login = newUser.Login
		u.Mail = newUser.Mail
		u.CreateGravatarLink()

		server.changeUserRoom(u, constants.DefaultRoom)
		logger.Print("Connexion d'un utilisateur")
	}
}
Esempio n. 5
0
//AddValue ajoute dans la BD
func (dbManager *DbManager) AddValue(bucketName string, key string, object fmt.Stringer) error {

	err := dbManager.db.Update(func(tx *bolt.Tx) error {
		bucket, err := tx.CreateBucketIfNotExists([]byte(bucketName))
		if err != nil {
			return err
		}

		encoded := object.String()

		return bucket.Put([]byte(key), []byte(encoded))
	})

	if err == nil {
		logger.Print("Ajout de " + key + " dans la bd")
	} else {
		logger.Error("Erreur lors de l'ajout dans la bd", err)
	}

	return nil
}