Chat Stuff

I'm trying to write a chat program where the first person to open the file creates the 'game', and subsequent openers of the file join it. For some reason, each person just seems to have their own individual chat room to talk to themselves. What am I doing wrong here?

var chatLog = "";
var userName = "Please Enter Your Name";
var myText = "";
var registered = false;
var windowRect = Rect (5, 5, 350, 350);

function OnGUI () {
    if (registered) {
        windowRect = GUI.Window (0, windowRect, PlayerWindow, "Colorful Planet Chat");
    } else {
        userName = GUILayout.TextField (userName);
        if (GUILayout.Button("Join the Chat!")) {
            MasterServer.ClearHostList();
            MasterServer.RequestHostList("WanillaCitrus001-chat");
            if (MasterServer.PollHostList().length == 0) {
                Network.useNat = !Network.HavePublicAddress();
                Network.InitializeServer(5000, 25002);
                MasterServer.RegisterHost("WanillaCitrus001-chat","cpChat","");
                networkView.RPC ("announcePlayer", RPCMode.All);
                registered = true;
            } else {
                var data : HostData[] = MasterServer.PollHostList();
                for (var element in data) {
                    if (element.gameName == "cpChat") {
                        if (Network.useNat) {
                            print("Using Nat punchthrough to connect to host");
                        } else {
                            print("Connecting directly to host");
                            Network.Connect(element.ip, element.port);
                        }
                    }
                }
                networkView.RPC ("announcePlayer", RPCMode.All);
                registered = true;
            }   
        }
    }
}

@RPC
function sendMessage () {
    if (myText != "") {
        chatLog += "\n" + userName + ": " + myText;
    }
}

@RPC
function announcePlayer () {
    chatLog += "\n" + userName + " joined the chat!";
}

function PlayerWindow (windowID : int) {
    GUI.Label (Rect(5,5,250,250), chatLog);
    myText = GUI.TextField (Rect(5, 250, 200, 20), myText);
    if (GUI.Button(Rect(210, 250, 45, 20), "Send")) {
        networkView.RPC ("sendMessage", RPCMode.All);
        myText = "";
    }
}

Can anyone help me? I would use the Networking Example, only I want this to be more of a lobby rather than have only the users of the current game chatting.

I am sorry I don't have time to go through your code heavily, but here are a couple of tips;

  1. MasterServer.RequestHostList doesn't return instantly, so you don't want to call it, then try to poll the list in the same event code (button click) as it will only try to poll once.

What I do, is call RequestHostList on app start, then provide a button for the user to refresh the list, which then calls it again. Then in my GUI display loop (no events, always runs) I poll the list and see if it contains any data, then I show that data (if any).

And actually, since you start a server automatically if none are found, that will be your problem. (just looked over the code again)

Also, for storing a chat list, it would actually be better to use an array (or specifically, an ArrayList) of strings, and simply add a new element to everyone's array when needed. This way you can prune really old messages and keep it clean, and you can also do per-line coloring or whatever else easier.

HTH,
-Jeremy