Example #1
0
func run() error {
	engine := qml.NewEngine()
	component, err := engine.LoadFile("chat.qml")
	if err != nil {
		fmt.Println("no file to load for ui")
		fmt.Println(err.Error())
		os.Exit(0)
	}

	context := engine.Context()
	context.SetVar("ctrl", &ctrl)

	win := component.CreateWindow(nil)

	win.Show() //show window
	ctrl.Root = win.Root()

	go ctrl.StartControlLoop()

	ctrl.UpdatedTextToUI <- "Hello " + peer.MyName + ".\nFor private messages, type the message followed by * and the name of the receiver.\n To leave the conversation type disconnect"

	go peer.RunServer(ctrl.UpdatedTextToUI, ctrl.UpdateUserList)

	if os.Args[1] != utils.GetMyIP() {
		go peer.IntroduceMyself(os.Args[1])
	} //connect to the first peer

	win.Wait()

	closing := peer.Message{"DISCONNECT", peer.MyName, utils.GetMyIP(), "", make([]string, 0), make([]string, 0)}
	closing.Send()
	return nil
}
Example #2
0
// sends message to the server
func (ctrl *Control) handleUserInput(input string) {

	log.Println("userInput")
	log.Printf("userInput got message: %s", input)
	whatever := strings.Split(input, "*")
	if input == "disconnect" {
		msg := peer.Message{"DISCONNECT", peer.MyName, "", "", make([]string, 0), make([]string, 0)}
		msg.Send()
		//os.Exit(1)
	} else if len(whatever) > 1 {
		msg := peer.Message{"PRIVATE", peer.MyName, "", whatever[0], make([]string, 0), make([]string, 0)}
		msg.SendPrivToUser(whatever[1], ctrl.UpdatedTextFromUI)
		ctrl.UpdatedTextToUI <- "(private) from " + peer.MyName + ": " + msg.MSG
	} else {
		msg := peer.Message{"PUBLIC", peer.MyName, "", whatever[0], make([]string, 0), make([]string, 0)}
		msg.Send()
		ctrl.UpdatedTextToUI <- peer.MyName + ": " + msg.MSG
	}

}