Introduction
Okay this is another XML thread which will help expand on As: Xml For Online Interactivity and for simplicities sake I am going to go over everything that we learnt from that thread as well. By the end of this tutorial you will have a working chat room and have some ideas on how to expand this further.
Getting started
Okay lets begin with actualy getting everything we need together. Use this checklist to make sure
- Macromedia Flash (I was using flash 8 but MX2004 should be fine)
- NowServer (www.nowcentral.com)
- Your IP address at hand (http://www.ninja-chi...com/ip%20getter.ph
p)
- A computer with an internet connection which is NOT behind a friewall.
Here is an explanation from BleeBlap :
'If you are behind a firewall the server will not be able to be accessed remotely. In english this means if there is more than one computer in the house connected to the same internet connection or if you have wireless internet you have a router. This means when the request gets sentout over the internet it gets sent from your ISP (internet service provider) back to your router which doesn't know what to do about it unless you mess with the router configuration. So putting localhost doesn't send it out over the internet and it works but using the isp does and it doesn't work. To fix it you can just unplug everybody else's internet and connect directly or set up the router which is different for each one and you should probably refer to the manual.'
Connecting
Okay now youve got everything together begin by starting up now server (I prefer the console version but it doesnt really matter)
Put this code in the first frame of a new flash document:
mySocket = new XMLSocket();
// Makes a new XML socket called 'mySocket'
mySocket.connect("localhost", 5525);
// Connects to the specified IP and port number (nowserver default = 5525)
- Note the above code may not work properly because of the word wrap on this post -
Change "localhost" to your IP if you intend on sharing this remotely
Wow easy youve just connected your first XML socket!
Setting up the stage
Okay because we are going to make a chatroom we need the following things on stage.
- A large dynamic text box with control var 'history' (Multiline and render as HTML)
- A small text box with control var 'messaged'
- A small text box with control var 'username'
- A button or movieClip with instance name 'button'
Now we are ready to start programming.
onConnect
the XML.onConnect method is used for when a connection is established. Add this to frame 1:
mySocket.onConnect = function(success) {
if (success) {
_root.history += " A Connection was successfully established <br><br>";
} else {
_root.history += " A Connection was not found <br><br>";
}
};
- Note the above code may not work properly because of the word wrap on this post -
Basically what this does is writes in the history box whether a connection was established or not.
Try it out now. If it doesnt find a connection make sure that the IP is correct and you have nowserver running.
The button
Okay now for the button. Again on frame 1 add the following:
_root.button.onPress = function() {
myXML = new XML(myXML);
// Creates a new XML object
myXML = "<?xml version=\"1.0\"?><Chat><Info channel=\"channel1\" user=\"" add _root.username add "\" data=\""+_root.messaged+"\" /></Chat>";
// Its very important you include the xml version and nowsever also requires you use channel and user for every XML
mySocket.send(myXML);
// Sends the XML you just created
this._visible = false;
// Makes the button invisible so the user cant flood the chat room (design feature not required)
};
- Note the above code may not work properly because of the word wrap on this post -
Okay the notes pretty much explains all of that so now you have a chat room which can send messages.
Add this to the bottom for the flood-protection
setIT = setInterval(function () {
_root.button._visible = true;
}, 3000);
- Note the above code may not work properly because of the word wrap on this post -
That doesnt need explaining.
Receiving data
This is a part which im quite iffy about so im not going to explain this. If someone (like fwe or inglor) is reading this perhpas they could write something about it below because I dont know it too well.
This is the code I use though
function parseXML(node) {
if (node.attributes.user != undefined) {
_root.history += node.attributes.user+" says: ";
_root.history += node.attributes.data+" <br>";
}
//You also need a loop here to cycle through all the messages recieved at once
//Just use the one nowserver made
if (node.hasChildNodes()) {
node = node.firstChild;
parseXML(node);
node = node.nextSibling;
while (node != null) {
parseXML(node);
node = node.nextSibling;
}
}
}
mySocket.onXML = parseXML;
- Note the above code may not work properly because of the word wrap on this post -
Okay also add this to the top of it all
_root.username = "Geust "+random(100);