Please help getting node.js working with AS3

For discussions about game development that does not fit in any of the other topics.
Post Reply
basil1492
Posts: 9
Joined: Fri May 02, 2014 6:42 pm

Please help getting node.js working with AS3

Post by basil1492 »

Hi, i've been testing various ways to get node.js working with AS3 but i'm hitting some serious problems (probably syntax related) in getting them to communicate with each other. Bypassed a few minor problems such as the cross domain thing by just adding my swf/html file directly to my global settings and managed to make a small chat server that works. I can now open up loads of versions of the swf and get it to send to each other but only using expressjs. This isn't a big problem but things like socket.emit or broadcast throws up loads of errors.

In the end i bypassed that by using a small function that i found via Google as below to send instead of broadcast to each of the others on the server. I even had to comment out the if client == sender line because it refused to send it to anyone.

function broadcast(message, sender) {
clients.forEach(function (client) {
//if (client === sender) return;
client.write(message);
});
process.stdout.write(message)
}

Anyway, I've been through all the videos including node for beginners series and even the building an html game and browser mmo. These all look great but writing AS3 in flash pro cs6 is way different to html5, half the commands just throw up errors. I've got it working using xmlsocket though to a degree. Now onto the things i can't do.

I can get it so that everyone logs on to the same server but my game is sort of like words with friends. I need to be able to just join 2 clients after the one has been invited and then for another 2 and another 2 to have their own connections. I also (probably being stupid here) want it so i can send different data to different functions on app.js. As an example on the tutorial you use

"socket.emit('recievedata',this.pos.x,this.pos.y,currentanimation,this.gamename);" and then add the recievedata function to app.js. Obviously with this method you can make as many functions as you want such as newplayer. When i try to send anything similar to the above its saying i can only send 1 argument. I know i could make it an array then use a few if else statements in app.js but surely theres a better way. Using the scrabble example when a player joins i would like them to trigger the join function in app.js, when they make a move it would need to send the move to another function on the server etc. My app needs to be able to send an array, 2 strings and a few other things at the end of each move.

Sorry for the long post and if i don't make a lot of sense, i'm fine with as3 but node.js is a lot more difficult for me, never knew JS before this either which doesn't help. As for getting 2 different languages to connect though, i'm finding that near enough impossible. I've spent about 100hrs the last 2 weeks trying to get this working with no luck. Any help will be very much appreciated.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Please help getting node.js working with AS3

Post by Jackolantern »

basil1492 wrote:I can get it so that everyone logs on to the same server but my game is sort of like words with friends. I need to be able to just join 2 clients after the one has been invited and then for another 2 and another 2 to have their own connections. I also (probably being stupid here) want it so i can send different data to different functions on app.js.
First let me state that I have not used raw sockets much in node. Most of my networked experience with node has been with Socket.io, but I have seen the raw sockets enough to know that it isn't that different.

As far as pairing players up, you have to build up a way to do that yourself. When a player connects, save their socket and name to some kind of JS structure. Then you could create a JS object and give different games a name combined of the two players in the game. If a player is waiting for a player, you would probably want to keep them in a separate object. Then when a socket event is received on the server, you can use the name (or possibly the socket ID, since I don't know if raw sockets allow attribution of arbitrary data to a socket the way Socket.io does) to find their game and the socket of the person they are playing against to emit out game state changes.
basil1492 wrote: When i try to send anything similar to the above its saying i can only send 1 argument. I know i could make it an array then use a few if else statements in app.js but surely theres a better way. Using the scrabble example when a player joins i would like them to trigger the join function in app.js, when they make a move it would need to send the move to another function on the server etc. My app needs to be able to send an array, 2 strings and a few other things at the end of each move.
Yes, it does only allow one argument to be sent. However, most of the time you will be wanting to send JSON strings, which can contain as much data as you need. My best advice is to create a well-documented interface for what data is sent on what events, what types, etc. Even if it is just for your own reference. You will be referring to it a lot.

As far as their being a "better way", I understand that. You don't want a pile of SWITCH statements for each possible action that comes in. Socket.io can suffer from the same lack of structure, and it even offers custom events (which I am not sure if raw sockets do or not). In Socket.io, you either have 1 listener with a huge SWITCH in it, or you have piles and piles of custom event callbacks. But even if raw sockets only offer one main event, you could possibly retrofit my Socket.io Dispatcher pattern for raw sockets. I wrote that to get away from the piles of Socket.io callbacks and SWITCH statements so that each JS module could simply register a listener method and the event callback would come directly into the object that needed the info.
basil1492 wrote:Sorry for the long post and if i don't make a lot of sense, i'm fine with as3 but node.js is a lot more difficult for me, never knew JS before this either which doesn't help. As for getting 2 different languages to connect though, i'm finding that near enough impossible. I've spent about 100hrs the last 2 weeks trying to get this working with no luck. Any help will be very much appreciated.
AS3 has a native JSON serializer/deserializer, which would probably be the best way to facilitate direct communication. However, I do know that AS3 supports binary socket connections, whereas browser-side Javascript does not, so I am not too sure there. Node.js adds binary support on top of JS, so node can handle that. And I am assuming you have likely already been through this since you have been working on this for so long, but I thought I would mention it just in case. Also be sure you have looked at results for "node.js communicate with Flex", since that is likely where you would find a lot more info about node communication with AS3.

Best of luck!
The indelible lord of tl;dr
basil1492
Posts: 9
Joined: Fri May 02, 2014 6:42 pm

Re: Please help getting node.js working with AS3

Post by basil1492 »

Thanks for the reply Jackolantern. I've been through those links, must admit the dispatcher one went so far above my head i couldn't even see it lol. I have used JSON before for one of my games so i can use that and the link you sent to communicate with flex, i'd already used that before. It kind of worked but whenever someone else connected it crashed. Something to do with not being able to work on the same socket or something. I did find a solution using a clients array but even though that got past the problem unfortunately when i added new clients they all changed their name to the last one.

I actually used the random name generator that was used on the tutorial and it would say something like player 345 has joined, i'd type a few things which would show on everyones screen as player 345>hello, but then if player 678 joined then typing on player 345s screen would show player 678>bye. Strange problem i couldn't get past so went to using express which seemed to work better.

Thanks for all the advice though. I'm going to have to look into sockets a lot more. I think the biggest problem here is that Adobe Air, AS3 etc wasn't really meant to interact with node.js. If i wanted to make an html5 game for eg there's literally loads of tutorials out for that but next to nothing showing for AS3 to node.js
User avatar
hallsofvallhalla
Site Admin
Posts: 12023
Joined: Wed Apr 22, 2009 11:29 pm

Re: Please help getting node.js working with AS3

Post by hallsofvallhalla »

basil1492 wrote:
I actually used the random name generator that was used on the tutorial and it would say something like player 345 has joined, i'd type a few things which would show on everyones screen as player 345>hello, but then if player 678 joined then typing on player 345s screen would show player 678>bye. Strange problem i couldn't get past so went to using express which seemed to work better.
AS3 and NodeJs, Sounds interesting! Sadly though I have not used them together and barely have used AS3.

The issue with the sockets reverting to last player name sounds like a socket.id issue. You should check to see if it is assigning the actual id or if it is even creating new ids for each instance.
basil1492
Posts: 9
Joined: Fri May 02, 2014 6:42 pm

Re: Please help getting node.js working with AS3

Post by basil1492 »

Thanks Halls, it could be the ID. I tried using the same method as the tutorial on here but when i used gameName i kept getting errors saying it wasn't defined. I've kind of decided to try and get it working now by sending an array. The array will contain all the things i need to send plus a string at index 0. That string will be the function name which i'm hoping i can use to call the function.

Kind of like the array will have [stringname of function, playername, playerposx, posy, movementspeed]. Just using this example as its similar to the game. If the stringname is movement i'll try and get it to call that function, say its called movement. That will then take the player name as its id (if i can work out how to get that working) and send the posx etc to the other client.

One thing im having trouble getting my head around though and i may have this all wrong. In the tutorial you call the initializeplayer function which sets the socket.clientname. For this to happen you need to be already connected. Is it possible to change the client name once the connection has already been made? I guess it must be but i'd have thought that once the socket was open it already has an id and if you changed that then wouldnt it disconnect you (as you'd have the original id before you changed the name)?
User avatar
hallsofvallhalla
Site Admin
Posts: 12023
Joined: Wed Apr 22, 2009 11:29 pm

Re: Please help getting node.js working with AS3

Post by hallsofvallhalla »

how would you not be connected and calling initializeplayer()? Maybe I do not understand the question.

You can change the ClientName at anypoint. For instance I could leave things the way they are as a guest name then when the player logs in it changes to the actual players registered name.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Please help getting node.js working with AS3

Post by Jackolantern »

basil1492 wrote:I actually used the random name generator that was used on the tutorial and it would say something like player 345 has joined, i'd type a few things which would show on everyones screen as player 345>hello, but then if player 678 joined then typing on player 345s screen would show player 678>bye. Strange problem i couldn't get past so went to using express which seemed to work better.
It sounds like you are not effectively storing the sockets as new connections come in. For example, your server will crash if you try to emit to a socket that has already disconnected (when a client disconnects, you must remove them from the object or other structure that you are storing them in so you won't call on it again). And the names will get mixed up if you aren't referencing the correct socket on the server-side.

I ran into these kinds of issues before I wrapped my head around the fact that this one server application was single-threaded and was handling all requests and dealing with all users. I was so used to PHP, where you could just forget about the other users and imagine that the script only deals with one user at a time. It took time before I came up with effective strategies for cross-user communication, and for storing user sockets for future communication.
basil1492 wrote:Thanks for all the advice though. I'm going to have to look into sockets a lot more. I think the biggest problem here is that Adobe Air, AS3 etc wasn't really meant to interact with node.js. If i wanted to make an html5 game for eg there's literally loads of tutorials out for that but next to nothing showing for AS3 to node.js
The beautiful thing about socket connections is that they communicate based on a standard protocol, meaning that AS3 shouldn't have to be built specifically for working with node. That said, it can be more difficult to work with low-level socket plumbing when you are trying to connect two different technologies that were not created with each other in mind. Socket.io and HTML5 do benefit from being made to work hand-in-hand and thus there is a nice abstraction layer on top of the low-level socket mechanics. But it is possible to do it without this convenience, as you have seen with some of your experiments. I will also say that Node.js resources for AS3 will be few and far between simply because many AS3 users use BlazeDS to connect to Java servers. But of course a Java server is far more complicated than node since it relies on a multithreaded model.
The indelible lord of tl;dr
basil1492
Posts: 9
Joined: Fri May 02, 2014 6:42 pm

Re: Please help getting node.js working with AS3

Post by basil1492 »

@Halls, sorry, my mistake, i totally misunderstood the clientname concept. I thought it was the connection name. I sort of thought of it as me connecting under the name bas, changing it while on and the connection name changing. Still not explaining it well, sort of like joining on port 80, changing the port number then expecting to still stay connected. I know its not that but thats how i thought it was working. Makes way more sense now you've explained it.

@JackOL, think you're right, see the paragraph above for what i was doing wrong. I've still got some work to do because for eg this line won't work in as3

socket.emit('recievedata',this.pos.x,this.pos.y,currentanimation,this.gamename);

That Blazeds looks useful if you know Java, i'll look into it a bit more but will probably stick to node.js. You are right about info being scarce though, almost non-existent.

Thanks both for all the help
User avatar
hallsofvallhalla
Site Admin
Posts: 12023
Joined: Wed Apr 22, 2009 11:29 pm

Re: Please help getting node.js working with AS3

Post by hallsofvallhalla »

The socket "class" uses its Id that it creates itself to distinguish itself. The client name is just a property of that class.
basil1492
Posts: 9
Joined: Fri May 02, 2014 6:42 pm

Re: Please help getting node.js working with AS3

Post by basil1492 »

Yes, told you i was new to this lol, didn't have a clue and in my defense i only started learning node.js/js late last week. Flash was easy to pick up, javascript and node.js not quite as easy. As it happens once i've got that done i've found a way to call functions from a string in js using the window and if typeof===function so i can now put a few functions in the app.js file and send it an array. I'll use index 0 then as the function to call with the other indexes as the args. Pretty sure it will work but not quite sure how quickly it will work if i get more than a few people linked to the server, guessing it won't effect it at all because it's turn based and will only pass info every few seconds.

Thanks again both for all the help, i wouldn't have got this sorted without it. Saying that i still have a bit to do. You probably explained the bit about connecting 2 players well enough for someone with a bit of knowledge on js but i'm going to have to look into it a bit more first, at least i've got a good starting point now.

Edit, not quite as easy as i thought lol. I've made the array in as3 which includes other arrays, strings etc and send it to node. When i log it though its coming through as a buffer. I've used json before and as was mentioned above it seems the best way to go but after trying for a few hrs now i can't seem to get it to work. I've also tried externalInterface and string2buffer and reversed.

Here's what i'm trying to do. AS3 sends an array with the 1st index being a string (function name). Node uses the switch case included below to decide what function to use. That function then sends the correct data to the other client. Eg, array[0] is onNew so it runs onNew with index[1] as the args which could be an array i need to send to the other client. fnstring would = index[0]. onNew would then send the args (which was index[1] to the other client which would update their side. In effect the args could be the 1st players posx, posy etc.

switch (fnstring) {
case "functionY": functionY(); break;
case "functionZ": functionZ(); break;
case "onNew": onNew(arg); break;

With the extInterface i tried this. AS3 code has this in the send button if(ExternalInterface.available) { ExternalInterface.call("jsTest", msg);} and in node.js i have function jsTest(arg) {return(arg);} (i did try alert(arg) but got an alert not defined error, also console.log still prints the buffer code) as does var str = jsTest(d);
console.log(str); which is in the onData(d) function.
Post Reply

Return to “General Development”