Be a Supporter!

As: Xml Sockets - Chatroom

  • 5,147 Views
  • 33 Replies
New Topic Respond to this Topic
Rustygames
Rustygames
  • Member since: May. 7, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
As: Xml Sockets - Chatroom 2005-10-19 11:15:32 Reply

AS: Main

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);


- Matt, Rustyarcade.com

Rustygames
Rustygames
  • Member since: May. 7, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to As: Xml Sockets - Chatroom 2005-10-19 11:16:44 Reply

Summing it up

Now your entire code on frame 1 should look like this...


_root.username = "Geust "+random(100);
mySocket = new XMLSocket();
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;
mySocket.connect("localhost", 5525);
mySocket.onConnect = function(success) {
if (success) {
_root.history += " A Connection was successfully established <br><br>";
}
};
_root.button.onPress = function() {
myXML = new XML(myXML);
myXML = "<?xml version=\"1.0\"?><Chat><Info channel=\"channel1\" user=\"" add _root.username add "\" data=\""+_root.messaged+"\" /></Chat>";
trace(myXML);
mySocket.send(myXML);
this._visible = false;
};
setIT = setInterval(function () {
_root.button._visible = true;
}, 3000);

And the stage should look something like this

www.ninja-chicken.com/XML.swf

(the above example will not work because the IP of the server must be the same IP as the host of the document)

If you want to try it out with a friend they will need the .swf file

Any questions post below
(Thanks Denvish)


- Matt, Rustyarcade.com

Toast
Toast
  • Member since: Apr. 2, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to As: Xml Sockets - Chatroom 2005-10-19 11:38:55 Reply

At 10/19/05 11:16 AM, Ninja-Chicken wrote:
www.ninja-chicken.com/XML.swf

Then if I got it right, I'm "Geust 29", right? :P
Learn how to spell "guest" before making those things..

I typed salghtdnh but nothing happened( I pressed that stupid button, also tried Enter).


BBS Signature
Rustygames
Rustygames
  • Member since: May. 7, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to As: Xml Sockets - Chatroom 2005-10-19 15:02:41 Reply

Can someone who does not have a home network and is willing to take 5 mins to help me test out my chatroom please contact me on msn
Thanks
- ninjachicken@hotmail.co.uk


- Matt, Rustyarcade.com

gorman2001
gorman2001
  • Member since: Aug. 18, 2002
  • Offline.
Forum Stats
Member
Level 14
Blank Slate
Response to As: Xml Sockets - Chatroom 2005-10-19 15:15:10 Reply

At 10/19/05 03:02 PM, Ninja-Chicken wrote: Can someone who does not have a home network and is willing to take 5 mins to help me test out my chatroom please contact me on msn
Thanks
- ninjachicken@hotmail.co.uk

well, hell, make it compatible with firewalls ! lol even if u just put a quick guide how to generally configure a router to open ports to your computer...


website :: hugostonge.com my job :: we+are

fwe
fwe
  • Member since: Jul. 24, 2003
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to As: Xml Sockets - Chatroom 2005-10-19 15:18:52 Reply

I have a firewall and people can connect to me. The only problem a firewall imposes is that you can't connect to yourself using hte IP address. Either use an internal IP (192.168.0.1) or localhost.


wtfbbqhax

gorman2001
gorman2001
  • Member since: Aug. 18, 2002
  • Offline.
Forum Stats
Member
Level 14
Blank Slate
Response to As: Xml Sockets - Chatroom 2005-10-19 15:20:12 Reply

or 127.0.0.1


website :: hugostonge.com my job :: we+are

Rustygames
Rustygames
  • Member since: May. 7, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to As: Xml Sockets - Chatroom 2005-10-19 15:40:15 Reply

At 10/19/05 03:18 PM, fwe wrote: I have a firewall and people can connect to me. The only problem a firewall imposes is that you can't connect to yourself using hte IP address. Either use an internal IP (192.168.0.1) or localhost.

No I have a router and noone can connect to me
If anyone knows how I can overcome this please help


- Matt, Rustyarcade.com

Rustygames
Rustygames
  • Member since: May. 7, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to As: Xml Sockets - Chatroom 2005-10-19 16:18:15 Reply

Okay if anyone would like to see an example of the above (though this is slightly modified but quite buggy)

http://www.ninja-chicken.com/XML.fla

Im off to watch star ship troopers ; )


- Matt, Rustyarcade.com

shazwoogle
shazwoogle
  • Member since: Sep. 27, 2004
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to As: Xml Sockets - Chatroom 2005-10-29 18:21:56 Reply

that suks.....

well its good in theory... i have loads of flash chat programs go to
---
www.gotoandplay.it
---

its heaps good =D

Rammer
Rammer
  • Member since: Jun. 8, 2003
  • Offline.
Forum Stats
Member
Level 33
Programmer
Response to As: Xml Sockets - Chatroom 2005-10-30 11:09:28 Reply

At 10/19/05 04:18 PM, Ninja-Chicken wrote: Okay if anyone would like to see an example of the above (though this is slightly modified but quite buggy)

http://www.ninja-chicken.com/XML.fla

Im off to watch star ship troopers ; )

that works perfectly on my computer, when i have more than 1 swf open :P


snyggys

Thomas
Thomas
  • Member since: Feb. 14, 2005
  • Offline.
Forum Stats
Member
Level 13
Musician
Response to As: Xml Sockets - Chatroom 2005-11-02 22:22:14 Reply

At 10/19/05 04:18 PM, Ninja-Chicken wrote: Okay if anyone would like to see an example of the above (though this is slightly modified but quite buggy)

http://www.ninja-chicken.com/XML.fla

Im off to watch star ship troopers ; )

Can you save it as a Flash MX 2004?

My room isn't connecting and I downloaded the NowServer thing...


wat a compessor wil raise the volume while lowering the db - chronamut

Thomas
Thomas
  • Member since: Feb. 14, 2005
  • Offline.
Forum Stats
Member
Level 13
Musician
Response to As: Xml Sockets - Chatroom 2005-11-02 22:32:48 Reply

Wait a second...

It works,but the part of sending a message doesn't.

Sorry for double

wat a compessor wil raise the volume while lowering the db - chronamut

Afro-Ninja
Afro-Ninja
  • Member since: Mar. 2, 2002
  • Offline.
Forum Stats
Moderator
Level 44
Game Developer
Response to As: Xml Sockets - Chatroom 2005-11-02 23:05:21 Reply

At 10/19/05 03:20 PM, gorman2001 wrote: or 127.0.0.1

which is the same as localhost


BBS Signature
Luis
Luis
  • Member since: Apr. 23, 2000
  • Offline.
Forum Stats
Member
Level 02
Melancholy
Response to As: Xml Sockets - Chatroom 2005-11-02 23:18:26 Reply

i once lost an xml socket in the washing machine.. it was disappointing.

oh get over it...
I JUST WANTED TO BE PART OF YOUR GROUP DISCUSSION get over my lame joke.


None

shazwoogle
shazwoogle
  • Member since: Sep. 27, 2004
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to As: Xml Sockets - Chatroom 2005-11-03 02:25:36 Reply

At 11/2/05 11:18 PM, _Luis_ wrote: i once lost an xml socket in the washing machine.. it was disappointing.

lol i actualy laughted at that =P

Thomas
Thomas
  • Member since: Feb. 14, 2005
  • Offline.
Forum Stats
Member
Level 13
Musician
Response to As: Xml Sockets - Chatroom 2005-11-03 17:43:16 Reply

:(

My only problem in your chatroom thing is that the Guest is undefined,so it doesn't work.


wat a compessor wil raise the volume while lowering the db - chronamut

pumpkinlover
pumpkinlover
  • Member since: Aug. 13, 2004
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to As: Xml Sockets - Chatroom 2006-06-14 16:56:46 Reply

the xml keeps giving me errors

myXML = "<?xml version=\"1.0\"?><Chat><Info channel=\"channel1\" user=\"" add _root.username add "\" data=\""+_root.messaged+"\" /></Chat>";

why?????

<deleted>
Response to As: Xml Sockets - Chatroom 2006-08-22 03:30:15 Reply

syntax error. fix the code

<deleted>
Response to As: Xml Sockets - Chatroom 2006-08-22 03:39:36 Reply

At 8/22/06 03:30 AM, Teh_Noobz_Killa wrote: syntax error. fix the code

o oops publish setting had to be flash 7. lol. This tutorial is awesome

JonBro
JonBro
  • Member since: Jul. 8, 2005
  • Offline.
Forum Stats
Member
Level 14
Blank Slate
Response to As: Xml Sockets - Chatroom 2006-09-24 19:29:30 Reply

On my computer, I can open two swf. files of the chatroom and have them interact with each other, but when I try it on two seperate computers, they don't connect. Is there something I did wrong?

And on a side note, I don't know how to make it where it says stuff like "JonBro signed off" or "JonBro signed on". Could someone post how to do this?


~

BleeBlap
BleeBlap
  • Member since: Mar. 8, 2005
  • Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to As: Xml Sockets - Chatroom 2006-09-24 20:24:23 Reply

At 10/19/05 11:15 AM, Ninja-chicken wrote: 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.'

Yeah that's all fine and dandy except I used the wrong word in the first sentence, which I think has confused a lot of people. I meant to say ROUTER not FIREWALL. While firewalls can cause problems if they prohibit access on the port that the server is running on that will (depending on your security system and settings) trigger a pop up that'll tell you something is trying to access the computer on the port and ask if you want to let it. If you are behind a ROUTER remote requests will be sent to your ip address which your router will recieve and not know what to do with so they won't make it to the server. I have since found a useful online guide that explains how to configure port forewarding on most routers. Link.

And JonBro, I would imagine it isn't working because either the server or the router isn't configured properly. When you open them on the same computer I assume you are using '127.0.0.1' or 'localhost' as the ip address. After configuring your router per the link above check here for what ip address you should use in the actionscript.

JonBro
JonBro
  • Member since: Jul. 8, 2005
  • Offline.
Forum Stats
Member
Level 14
Blank Slate
Response to As: Xml Sockets - Chatroom 2006-09-26 17:30:32 Reply

At 9/24/06 08:24 PM, BleeBlap wrote: And JonBro, I would imagine it isn't working because either the server or the router isn't configured properly. When you open them on the same computer I assume you are using '127.0.0.1' or 'localhost' as the ip address. After configuring your router per the link above check here for what ip address you should use in the actionscript.

Hm... That helped, but it still doesn't work. How would the IP address look in the ActionScript?

And I still don't see how to make it to where you see when someone signs on or off.

I ran into a pretty big glitch, too:
If I chat between 3 swf. chatroom files on my computer, and then on the first, I post the first message, it doesn't connect with any of the other swf. files. After that, when I post a message on the second file, it only connects with the first one. If anyone else has experienced this problem and knows how to fix it, I'd like to know.


~

Mehrdad14
Mehrdad14
  • Member since: Dec. 5, 2005
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to As: Xml Sockets - Chatroom 2007-05-06 19:16:21 Reply

myXML = "<?xml version=\"1.0\"?><Chat><Info channel=\"channel1\" user=\"" +_root.username +"\" data=\""+_root.messaged+"\" /></Chat>";

change add to + :)


Fate. Strength. Intelligence.

senior-twinki
senior-twinki
  • Member since: Jun. 26, 2007
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to As: Xml Sockets - Chatroom 2007-06-26 23:43:59 Reply

NG could use a chatroom X __X

ultim8dude
ultim8dude
  • Member since: Nov. 29, 2006
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to As: Xml Sockets - Chatroom 2007-06-26 23:47:08 Reply

At 6/26/07 11:43 PM, senior-twinki wrote: NG could use a chatroom X __X

it has a chatroom..look at the "chat" link right above the audio portal, and why did you bump this?


Check out TreeJumper at here

tgw1996
tgw1996
  • Member since: Dec. 15, 2007
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to As: Xml Sockets - Chatroom 2008-05-29 15:50:23 Reply

I need some help.
I got the server and everything but no matter what message I send it just says

level0.messaged

in the history box.
Any Help?

Nicholas-Deary
Nicholas-Deary
  • Member since: May. 12, 2006
  • Offline.
Forum Stats
Member
Level 39
Animator
Response to As: Xml Sockets - Chatroom 2008-05-29 15:54:07 Reply

Dude, this is just the chatroom I need for NGP3 =D


BBS Signature
tgw1996
tgw1996
  • Member since: Dec. 15, 2007
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to As: Xml Sockets - Chatroom 2008-05-29 16:03:47 Reply

So does any one have any help for me or not.

<deleted>
Response to As: Xml Sockets - Chatroom 2010-05-07 19:11:56 Reply

Sorry for bumping. I know the last post is from 2008, but I have very relevant info/questions that maybe Flash regulars could help me out with. I AM behind a router. I think what I need to do is to forward a port,which I can do. What ports need forwarding and should it be TCP or UDP?