Contains a complete chat example. It can either run in the editor using the old unity network or in the browser as WebGL/HTML5 using WebRtc. The chat app will report during start which system it uses. The user can enter a room name and click the "Open room" button to start a server and wait for incomming connections or use the "Join room" button to join an already existing room. Note: Unity network uses random numbers / guid to idendify a server. The room name entered by the user will be ignored. As the system implements a server/client style connection all messages will first be sent to the server and the server delivers it to each client. The server side ConnectionId is used to idendify a user.
Inheritance: MonoBehaviour
Example #1
0
    // Reset the chat app
    public void ResetMessageScreen(bool resetMessages, bool clearDecisions, bool resetDecisions, bool resetSendBox)
    {
        if (!chatApp)
        {
            chatApp = FindObjectOfType <ChatApp>();
        }

        if (chatApp && chatApp.messageScreenInstance)
        {
            if (resetMessages)
            {
                chatApp.messageScreenInstance.ResetMessages(chatApp.activeChat);
            }
            if (clearDecisions)
            {
                chatApp.messageScreenInstance.ClearDecisions();
            }
            if (resetDecisions)
            {
                chatApp.messageScreenInstance.ResetDecisions(chatApp.activeChat);
            }
            if (resetSendBox)
            {
                chatApp.messageScreenInstance.sendBox.Reset(chatApp.activeChat);
            }
        }
    }
Example #2
0
 //Start Protocol
 private void StartProtocol(TcpClient client)
 {
     ChatApp.LogMessage("Protocol Starting");
     WriteMessageToClient(client, SAY);
     while (!stop)
     {
         ChatApp.ChangeInputState(true);
         string msg = GetMessageFromClient(client);
         ChatApp.ReceiveMessage(msg);
         if (!msg.Equals("SAY") && Motu.Instance.IsInitialized())
         {
             Motu.Instance.PlaySentence(msg, ICI, IWI);
         }
         else
         {
             ChatApp.ReceiveMessage("MOTU not initialized yet");
         }
         ChatApp.ChangeInputState(false);
         while (msgBuffer.Equals(""))
         {
             ;
         }
         WriteMessageToClient(client, msgBuffer);
         lock (lockObj)
         {
             msgBuffer = "";
         }
     }
 }
Example #3
0
        public BuddyListWindowViewModel(ChatApp app) : this()
        {
            _app = app;

            // Listen to app Favourites
            app.Favourites.CollectionChanged += Favourites_CollectionChanged;
        }
Example #4
0
    // Coroutine to make the contact come online after response time
    private IEnumerator NotifyContact(Contact contact)
    {
        contact.offlineTimer = Time.time + contact.activeTime;

        if (contact.state == Contact.State.Offline)
        {
            ChatApp chat = FindObjectOfType <ChatApp>();
            contact.state = Contact.State.Notified;
            yield return(new WaitForSeconds(contact.responseTime));

            contact.state = Contact.State.Online;
            if (chat && chat.messageScreenInstance)
            {
                chat.messageScreenInstance.SetStatus(chat.activeChat);
            }

            while (contact.state == Contact.State.Online)
            {
                if (Time.time > contact.offlineTimer)
                {
                    contact.state = Contact.State.Offline;
                    if (chat && chat.messageScreenInstance)
                    {
                        chat.messageScreenInstance.SetStatus(chat.activeChat);
                    }
                }
                yield return(null);
            }
        }
    }
Example #5
0
    // Coroutine to make the contact come online
    private IEnumerator GetOnline(Contact contact)
    {
        contact.offlineTimer = Time.time + contact.activeTime;

        ChatApp chat = FindObjectOfType <ChatApp>();

        contact.state = Contact.State.Online;
        if (chat && chat.messageScreenInstance)
        {
            chat.messageScreenInstance.SetStatus(chat.activeChat);
        }

        while (contact.state == Contact.State.Online)
        {
            if (Time.time > contact.offlineTimer)
            {
                contact.state = Contact.State.Offline;
                if (chat && chat.messageScreenInstance)
                {
                    chat.messageScreenInstance.SetStatus(chat.activeChat);
                }
            }
            yield return(null);
        }
    }
Example #6
0
                public BuddyListEntry(ChatApp app, Buddy buddy)
                {
                    _app        = app;
                    Buddy       = buddy;
                    OpenCommand = new AlwaysExecutableCommand(() => _app.ShowChatForBuddy(Buddy));

                    // Buddy.PropertyChanged += (_, __) => PropertyChanged(this, new PropertyChangedEventArgs(nameof(Buddy)));
                }
Example #7
0
    // Send a new message and apply the players choice
    public void MakeDecision(int id, Chat chat)
    {
        chat.story.MakeDecision(id);

        ChatApp.ClearDialogueOptions(chat);
        ResetMessageScreen(false, true, false, false);

        UpdatePassage(chat);
    }
Example #8
0
    // Send message to the phone
    public void Send(Message message, bool preChat)
    {
        Phone phone = Transform.FindObjectOfType <Phone>();

        ChatApp.SaveMessage(message);

        if (!preChat)
        {
            foreach (Contact c in message.chat.contacts)
            {
                c.AddMessageToRead(message);
                StartCoroutine(NotifyContact(c));
            }

            if (message.contact != You)
            {
                ChatApp chat = FindObjectOfType <ChatApp>();
                if (chat)
                {
                    if (chat.activeChat != message.chat)
                    {
                        phone.GetNotified(0);
                        message.chat.unreadMessages++;
                    }
                    else
                    {
                        phone.GetNotified(1);
                    }
                }
                else
                {
                    phone.GetNotified(0);
                    message.chat.unreadMessages++;
                }

                StartCoroutine(GetOnline(message.contact));
            }
            else
            {
                phone.GetNotified(1);
            }

            ResetMessageScreen(true, false, true, false);
            if (!chatApp)
            {
                chatApp = FindObjectOfType <ChatApp>();
            }
            if (chatApp)
            {
                chatApp.UpdateContacts(message);
            }

            message.chat.CheckIfFinished();
        }
    }
Example #9
0
        public void Connect(int userId, int conversationId)
        {
            using (var ctx = new ChatApp()) {
                var conversation = ctx.Conversations.Where(p => p.Members.Any(d => d.UserID == userId)).FirstOrDefault(p => p.Id == conversationId);

                if (conversation == null)
                {
                    return;
                }

                Groups.Add(Context.ConnectionId, conversation.Name);
            }
        }
Example #10
0
 public void SaveSession(string name, string sessionId)
 {
     // Call the addNewMessageToPage method to update clients.
     using (var context = new ChatDemoAppEntities()) {
         var chatTbl = new ChatApp
         {
             ChatSession = sessionId,
             UserName    = name
         };
         context.ChatApps.Add(chatTbl);
         context.SaveChanges();
     }
 }
Example #11
0
        public IEnumerable <object> Conversation(int userId)
        {
            using (var ctx = new ChatApp())
            {
                var user = ctx.Users.Include("Conversations").FirstOrDefault(p => p.UserID == userId);

                return(user.Conversations.Select(p => new
                {
                    id = p.Id,
                    name = p.Name
                }).ToList());
            }
        }
Example #12
0
        public IEnumerable <object> Message(int conversationId)
        {
            using (var ctx = new ChatApp())
            {
                var conversation = ctx.Conversations.Include("Messages").Include("Messages.User").FirstOrDefault(p => p.Id == conversationId);

                return(conversation.Messages.Select(p => new
                {
                    id = p.MessageID,
                    text = p.Text,
                    user = p.User.UserName
                }).ToList());
            }
        }
Example #13
0
        //Stop all sessions
        public void StopSession()
        {
            stop = true;
            if (listener != null)
            {
                listener.Stop();
            }
            if (listenThread != null)
            {
                listenThread.Abort();
                listenThread = null;
            }

            ChatApp.LogMessage("Server Stopped");
        }
Example #14
0
        public int Login([FromBody] LoginModel model)
        {
            using (var ctx = new ChatApp())
            {
                var user = ctx.Users.FirstOrDefault(p => p.UserName == model.UserName && p.Password == model.Password);
                if (user == null)
                {
                    user = ctx.Users.Add(new User
                    {
                        UserName = model.UserName,
                        Password = model.Password
                    });
                    ctx.SaveChanges();
                }

                return(user.UserID);
            }
        }
Example #15
0
        //Infinite listening loop
        void ListenConnections()
        {
            if (IpAdress.Equals("localhost"))
            {
                listener = new TcpListener(IPAddress.Any, PortNumber);
            }
            else
            {
                IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
                IPAddress   ipAddress  = null;
                foreach (IPAddress addr in ipHostInfo.AddressList)
                {
                    if (addr.ToString().Equals(IpAdress))
                    {
                        ipAddress = addr;
                    }
                }
                if (ipAddress == null)
                {
                    ChatApp.LogMessage("Unable to listen in the specified address");
                    return;
                }
                else
                {
                    listener = new TcpListener(ipAddress, PortNumber);
                }
            }

            listener.Start();
            ChatApp.LogMessage("Listening to connections...");

            try
            {
                TcpClient client = listener.AcceptTcpClient();
                ChatApp.LogMessage("Client received");
                StartProtocol(client);
            }
            catch (Exception)
            {
                return;
            }
        }
Example #16
0
        public int Conversation([FromBody] ConversationModel model)
        {
            using (var ctx = new ChatApp())
            {
                var memberNames     = model.Members.Split(';');
                var newConversation = new Conversation
                {
                    Name        = model.Name,
                    Members     = new List <User>(),
                    CreatedDate = DateTime.Now
                };
                var members = ctx.Users.Where(p => memberNames.Contains(p.UserName)).ToList();
                var user    = ctx.Users.Find(model.UserId);
                members.Add(user);
                newConversation.Members = members;

                ctx.Conversations.Add(newConversation);
                ctx.SaveChanges();

                return(newConversation.Id);
            }
        }
Example #17
0
        public void NewMessage(int userId, int conversationId, string message)
        {
            using (var ctx = new ChatApp())
            {
                var user         = ctx.Users.Find(userId);
                var conversation = ctx.Conversations.Include("Members").Include("Messages").FirstOrDefault(p => p.Id == conversationId);

                if (conversation == null)
                {
                    return;
                }

                conversation.Messages.Add(new Message {
                    CreatingDate = DateTime.Now,
                    Text         = message,
                    User         = user
                });
                ctx.SaveChanges();

                Clients.Group(conversation.Name).broadcastMessage(user.UserName, message);
            }
        }
Example #18
0
    // Load all messages and decisions from the current passage
    public void UpdatePassage(Chat chat)
    {
        if (chat.story)
        {
            foreach (string message in chat.story.GetMessages())
            {
                SendMessage(chat, message);
            }

            // Reset all decisions
            ChatApp.ClearDialogueOptions(chat);

            Decision[] newDecisions = chat.story.GetDecisions();
            foreach (Decision d in newDecisions)
            {
                MessageOption option = ScriptableObject.CreateInstance <MessageOption>();
                option.name    = chat.story.currentPassage.ToString();
                option.chat    = chat;
                option.message = d.message;
                option.id      = d.link;
                ChatApp.AddDialogueOption(option);
            }
        }
    }
Example #19
0
    public IEnumerator SendMessages()
    {
        // Colleagues
        if (PlayerPrefs.GetInt("Theme") == 0)
        {
            yield return(SendMessages(Colleagues, ColleagueEndings[7], false));
        }
        else
        {
            yield return(SendMessages(Colleagues, ColleagueEndings[9], false));
        }

        if (PlayerPrefs.GetInt("Food") == 0)
        {
            yield return(SendMessages(Colleagues, ColleagueEndings[5], false));
        }
        else
        {
            yield return(SendMessages(Colleagues, ColleagueEndings[3], false));
        }

        if (PlayerPrefs.GetInt("Drinks") == 0)
        {
            yield return(SendMessages(Colleagues, ColleagueEndings[1], false));
        }
        else
        {
            yield return(SendMessages(Colleagues, ColleagueEndings[0], false));
        }

        if (PlayerPrefs.GetInt("Music") == 0)
        {
            yield return(SendMessages(Colleagues, ColleagueEndings[8], false));
        }
        else
        {
            yield return(SendMessages(Colleagues, ColleagueEndings[2], false));
        }

        int likes = 0;

        if (PlayerPrefs.GetInt("Theme") == 0)
        {
            likes++;
        }
        if (PlayerPrefs.GetInt("Food") == 1)
        {
            likes++;
        }
        if (PlayerPrefs.GetInt("Drinks") == 0)
        {
            likes++;
        }
        if (PlayerPrefs.GetInt("Music") == 0)
        {
            likes++;
        }

        if (likes > 2)
        {
            yield return(SendMessages(Colleagues, ColleagueEndings[6], false));
        }
        else
        {
            yield return(SendMessages(Colleagues, ColleagueEndings[4], false));
        }

        yield return(WaitForChatToFinish(Colleagues));

        // Boss
        if (PlayerPrefs.GetInt("Drinks") == 0)
        {
            yield return(SendMessages(Boss, BossEndings[0], false));
        }
        else
        {
            yield return(SendMessages(Boss, BossEndings[1], false));
        }

        yield return(WaitForChatToFinish(Boss));

        // Migrants
        if (PlayerPrefs.GetInt("Theme") == 0)
        {
            yield return(SendMessages(Migrant, MigrantsEndings[8], false));
        }
        else
        {
            yield return(SendMessages(Migrant, MigrantsEndings[9], false));
        }

        if (PlayerPrefs.GetInt("Food") == 0)
        {
            yield return(SendMessages(Migrant, MigrantsEndings[4], false));
        }
        else
        {
            yield return(SendMessages(Migrant, MigrantsEndings[3], false));
        }

        if (PlayerPrefs.GetInt("Drinks") == 0)
        {
            yield return(SendMessages(Migrant, MigrantsEndings[1], false));
        }
        else
        {
            yield return(SendMessages(Migrant, MigrantsEndings[0], false));
        }

        if (PlayerPrefs.GetInt("Music") == 0)
        {
            yield return(SendMessages(Migrant, MigrantsEndings[5], false));
        }
        else
        {
            yield return(SendMessages(Migrant, MigrantsEndings[2], false));
        }

        likes = 0;
        if (PlayerPrefs.GetInt("Theme") == 1)
        {
            likes++;
        }
        if (PlayerPrefs.GetInt("Food") == 0)
        {
            likes++;
        }
        if (PlayerPrefs.GetInt("Drinks") == 1)
        {
            likes++;
        }
        if (PlayerPrefs.GetInt("Music") == 1)
        {
            likes++;
        }

        if (likes > 2)
        {
            yield return(SendMessages(Migrant, MigrantsEndings[7], false));
        }
        else
        {
            yield return(SendMessages(Migrant, MigrantsEndings[6], false));
        }

        yield return(WaitForChatToFinish(Migrant));

        // Afia
        if (likes > 2)
        {
            yield return(SendMessages(Afia, AfiaEndings[1], false));
        }
        else
        {
            yield return(SendMessages(Afia, AfiaEndings[0], false));
        }

        yield return(WaitForChatToFinish(Afia));

        ChatApp chatApp = FindObjectOfType <ChatApp>();

        if (chatApp)
        {
            chatApp.UpdateLastMessages();
        }
    }
        static void Main(string[] args)
        {
            ChatApp chatapp = new ChatApp();

            chatapp.Start();
        }
Example #21
0
        public BuddyListWindowViewModel()
        {
            _app = null !;

            BuddyListViewModels.Add(FavouritesList);
        }