[PLUGIN] Zack0Wack0's Socket.io mod

Post your completed addons to share with the community

Post » Tue Dec 11, 2012 2:49 am

I would like to know, how to try with Juantar's demo?
B
96
S
25
G
20
Posts: 3,055
Reputation: 22,644

Post » Tue Dec 11, 2012 10:34 am

Actually, if you're using node.js, there is no need to have that dependency, your server will serve the client automatically.

As for multiple instances issue, try to change the id of the plugin and insert multiple instances of the same plugin, each instance with unique id. if that breaks the project, besides renaming the id, try to rename the socket variable in which connection is stored, so you get something like:

var socket = io.connect('http://localhost/socket')
    , socket2 = io.connect('http://localhost/socket2');

socket.on('some_event', function () {
    // do something
});

socket2.on('other_event', function () {
    // do something
});



@Johannesalfa

It should work with juantar's demo, it's pretty much the same plugin, i added some actions and stuff and edited it so it doesn't break when you minify it.JohnnySheffield2012-12-11 11:41:22
Be nice until it's time to not be nice
B
36
S
9
G
9
Posts: 293
Reputation: 6,652

Post » Wed Feb 06, 2013 5:37 am

Thanks for this great plugin but I'm having trouble understanding the LastData bit.

Why not present data?

Consider:

io.sockets.on('connection', function (client) {
        client.userid = UUID();
        client.emit('onconnection', {id: client.id});
        console.log('\t socket.io:: player ' + client.userid + ' connected');

        client.on('disconnect', function () {
            console.log('\t socket.io:: client disconnected ' + client.userid );

        });
     
});

I do a connection, then check if any data is available. If I receive an event named 'onconnection' I spit out the alert with Socket.LastData.

This comes up as 'websocket' before another alert '[Object] [object]' which is the one I want.

What is this 'websocket', why do we look one step back in time?

I must be missing something here!
B
5
Posts: 39
Reputation: 678

Post » Wed Feb 06, 2013 11:51 am

[QUOTE=aceofpack] Thanks for this great plugin but I'm having trouble understanding the LastData bit.

Why not present data?

Consider:

io.sockets.on('connection', function (client) {
        client.userid = UUID();
        client.emit('onconnection', {id: client.id});
        console.log('\t socket.io:: player ' + client.userid + ' connected');

        client.on('disconnect', function () {
            console.log('\t socket.io:: client disconnected ' + client.userid );

        });
     
});

[/QUOTE]

Is this your server-side code?

[QUOTE=aceofpack]
I do a connection, then check if any data is available. If I receive an event named 'onconnection' I spit out the alert with Socket.LastData.

This comes up as 'websocket' before another alert '[Object] [object]' which is the one I want.

What is this 'websocket', why do we look one step back in time?

I must be missing something here!

[/QUOTE]

Ok, you probably don't want to use the socket.LastData expression, it behaves really strange, and messes up the data... If you read the explanation of the Socket.LastData expression it says:

(Get the last chunk of data that was received via the socket. (Warning: this exression actually manupalates the data stack, be careful when evaluating it in Construct)

Meaning: Don't use it!


What you want to do is:

On Data ---> Split data received
    ---> Access your data with Socket.LastDataElement(index)


The "Split data received" Action actually makes the Data.split(','); so it returns an array.


If you don't want to split the data, and just access the last data recived do the

On Data ---> Access your data with Socket.LastDataElement(0)



This is actually the main reason why this plugin isn't the best it can be, it doesen't manipulates recived data on the best possible way.

Reason for this is that this plugin was originally written by Zack0Wack0, then edited by Juantar, then by some other members, and lastly by me... I added some functionality to it, made it minifiable, but it actually needs an overhaul. A lot of stuff isn't coded the best it could be!

I'm currently waiting for the socket.io 1.0 to become official, because it makes some serious changes to the Socket.io module and possibly a different API, so probably would be waste of time to overhaul it now!


I hope i helped,

Cheers!

Be nice until it's time to not be nice
B
36
S
9
G
9
Posts: 293
Reputation: 6,652

Post » Wed Feb 06, 2013 11:52 pm

Thanks a lot for your help, much appreciated.

(Yes, that was my server code btw).

I was taking a look at the runtime.js and was thinking if splitting the data by commas was the only way.

Thanks for the clarification. I was thinking of editing it to work with JSON in the way this article discusses:

http://buildnewgames.com/optimizing-websockets-bandwidth/

But maybe for now I will concentrate on using CSV's and working on the client-side predicition and interpolating entities parts of multiplayer:

http://www.gabrielgambetta.com/?p=11 (3 parts)

I'll get pong up on a server and share to test if we can get acceptable real-time results.
B
5
Posts: 39
Reputation: 678

Post » Thu Feb 07, 2013 8:53 am

Great resources, thanks for sharing!

My test have shown that with using socket.io i get acceptable real-time results (Acceptable for me), with hosting on nodejitsu (i'm in europe).

It would be probably better if i'm using my own local server, but that's on the to-do list!

Also, i'm not sure does it affect the performance, but for my games, i edit the plugin and add my own events to the edittime.js and runtime.js, so i don't have to split data, then compare the first element of array then do some actions. That way, the code looks messy and it's not easy to maintain.

What i do is add a condition to edittime.js somwhere around line 22 like this:

AddCondition(5, cf_trigger, "On my event", "My Category", "On My event", "Triggered when my event", "MyEvent");



To the runtime.js somwhere around line 120:

          socket["on"]
          (
              'MyEvent',
              function(data)
              {
              instance.dataStack.push(data);
              runtime.trigger(pluginProto.cnds.MyEvent,instance);
              }
          );


and finally add a condition to the condition list around line 265:

     cnds.MyEvent = function()
     {
          return true;
     };


So i my code looks like:

On My event ---> DoAction (Split data or not, your choice)


And not like:

On Data ---> Split Data
   ---> If LastDataElement(0)="My Event" ---> DoAction

As for making it work with JSON, maybe look into the following plugins:

Load JSON

Hash Table

And perhaps use them in combination with this plugin!


Also, if you do some edits to the plugin, i'd be really grateful if you'd share the edits so i can implement them in the plugin, and if you have some test to share that would be great also!

Cheers!

JohnnySheffield2013-02-07 08:55:24
Be nice until it's time to not be nice
B
36
S
9
G
9
Posts: 293
Reputation: 6,652

Post » Fri Feb 08, 2013 9:12 am

Thanks again Johnny, I will share my progress as it's good for us to advance this concept to a very decent solution.

B
5
Posts: 39
Reputation: 678

Post » Sat Feb 09, 2013 11:03 pm

Hey Johnny/all,

https://www.dropbox.com/s/095i4mjtkd9pe2y/socket-io-ace.zip

Not sure if I should do a new post but I've amended the plugin with the following feature:

LastDataJSONStringify which works well in combo with the hash table plugin:

http://www.scirra.com/forum/plugin-hash-table_topic47637_post298284.html

So an example use would be to setup a hash table to store incoming values sent from node such as:



Then, on an event, populate the hash table with the JSON string:



You can then access the data using:

int(OtherPlayerData.AtKeys("y"))

Server code would look something like:

this.broadcast.emit("move player", {id: movePlayer.id, x: movePlayer.getX(), y: movePlayer.getY()});

I'm still working on this so likely to release some more stuff soon...

Sorry for the short explanation but might as well release often just in case it helps folks :)aceofpack2013-02-09 23:09:12
B
5
Posts: 39
Reputation: 678

Post » Wed May 01, 2013 7:28 pm

Whats the most updated "mainstream" version?
B
7
S
3
G
1
Posts: 154
Reputation: 1,812

Post » Thu May 02, 2013 3:01 pm

I would first see will the official websocket plugin suits your needs. if not, there's aceofpack mod, with possibility to send and recieve JSON, but with unstable 'onanyevent' triggers, or my mod, but without json and anyevent. your best bet is to use either version and adapt it to your needs!

Cheers!
Be nice until it's time to not be nice
B
36
S
9
G
9
Posts: 293
Reputation: 6,652

PreviousNext

Return to Completed Addons

Who is online

Users browsing this forum: No registered users and 3 guests