[Updated w/Post]Using C2 WebSocket Plugin + WS?

Discussion and feedback on Construct 2

Post » Wed Nov 27, 2013 8:27 am

Update: Please see the latest post, I'm trying to figure out this whole "events" thing with WebSocket modules and how to work with them using C2's WebSocket plugin, which can only send text strings.

So I've been having some issues getting the WebSocket plugin to play with Nodejs and Socket.IO.

My server file is as simple as I can make it:
      var server = require('http').createServer(server)
      var io = require('socket.io').listen(server);

      server.listen(8888);

The WebSocket plugin's connect action is defined to connect to the URL: "ws://localhost:8888"

What I get after running starting the server, and running my C2 game, is: "debug - destroying non-socket.io upgrade" spit out in the command prompt.

I'm not sure if I am using the wrong URL, or what. I've looked up the issue and no one has a fix. There was another user with the same problem but no one commented on the post and he reported fixing it himself (without description) and left the forum.

I'd really appreciate help. There's a surprising lack of people experimenting with WebSockets and C2. :PDatapawWolf2013-12-06 02:37:59
ImageImageImageImage
B
62
S
19
G
51
Posts: 633
Reputation: 30,826

Post » Wed Nov 27, 2013 1:04 pm

Why use socket.io? Why not just use websockets directly, like the ws node.js module? Almost every browser that can run HTML5 games also supports Websockets (I think the only exception is IE9, but IE10+ supports them). Usually you'd only use socket.io if you really need fallbacks, but I'd say there's no real need for that.Ashley2013-11-27 13:05:32
Scirra Founder
B
395
S
233
G
88
Posts: 24,376
Reputation: 193,842

Post » Wed Nov 27, 2013 5:41 pm

@Ashley oh ok. Someone had recommended Socket.IO, so I went ahead and tried it out.

What is the difference, then, between all the WebSocket Nodejs modules? There are a handful, and Socket.IO seemed to be the most popular.

But I will give ws a try. I'd seem some examples using it and it seems about the same code-wise as Socket.IO.

Thanks!
ImageImageImageImage
B
62
S
19
G
51
Posts: 633
Reputation: 30,826

Post » Thu Dec 05, 2013 9:38 pm

@Ashley I'm sorry to bring this back buuut...

So have been experimenting with Nodejs and the WS module like you suggested.

However, I have a few more questions, which I wouldn't mind any answers to.

So Construct 2's WebSocket plugin sends text strings to the connected server, correct?

From there, I ran one successfully and connected my game client without issue.

Messages were also working fine, passing from Client to Server and vice-versa.

What I would like to know, is what these "custom event" emitters are I see in such modules as Socket.IO:

socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });


And if this is possible since, as with the client above, there is more than just a message, it seems.

Right now, I can only send text strings, which is nice, but they only go to the server under "message," which means I can't send, say, an event emission "move" from the C2 client instead having to parse for the first part of a message.

Am I right in that C2 can only emit data to the server in the form of a "message"? Or am I just crazy? What I would like to be able to do is something like:

socket.on('moveChar', function (data) {
    console.log(data);
});


Instead of using something like:
socket.on('message', function (data) {
    console.log(data);
    [parse data for string 'moveChar'>
});


I'm fully prepared to work either way, I just would like to know if what I'm talking about is possible or if I'm just crazy and should go back to doing research.
ImageImageImageImage
B
62
S
19
G
51
Posts: 633
Reputation: 30,826

Post » Fri Dec 06, 2013 12:44 pm

You need to invent your own protocol. A really simple example would be to send strings like "message|data", e.g. "move|100,200". Then you can parse out each part of the message and pass the 'data' part to the correct function. This is probably what socket.io does under the hood, but you need to do it yourself when using WebSockets directly.
Scirra Founder
B
395
S
233
G
88
Posts: 24,376
Reputation: 193,842

Post » Fri Dec 06, 2013 8:30 pm

Alrighty. Thank you for the reply! I will work on that, then. But, I mean, I think most modules work this way and/or whatever is packaged with Nodejs, not just Socket.IO. I think it would be a great feature to have in Construct 2.
ImageImageImageImage
B
62
S
19
G
51
Posts: 633
Reputation: 30,826

Post » Sat Dec 07, 2013 12:17 pm

The intent of the WebSocket object is to let you do what you want with the communication channel. Different servers communicate in different ways, and if we invented our own message format it would mainly serve to break compatibility with 99% of the websocket servers out there.
Scirra Founder
B
395
S
233
G
88
Posts: 24,376
Reputation: 193,842

Post » Sat Dec 07, 2013 9:26 pm

Ah ok then. Thanks for all the information! I'm starting to understand WebSockets and C2's handling a little better.

If I can get a basic game type going I'll be sure to place it up so others can get a feel for it. :)
ImageImageImageImage
B
62
S
19
G
51
Posts: 633
Reputation: 30,826

Post » Sat Dec 07, 2013 9:42 pm

I am also doing some work with Websocket-related things at the moment. So far, a lot of it is rather irrelevant to Construct 2 minus the plugin you choose to use.

Websockets are exactly what they are- a websocket. This means that a lot of the extra features are just purely not present.

Socket.IO brings in extra functionality. It uses websockets but also has things like rooms(channels) and fallback policies*. A reason to not use Websockets is that if you don't need that functionality or you want to custom add what you want.


*Fallback policies are presented when a client fails to utilize websockets. This can allow non-websocket accepting browsers and mobile devices to still have a websocket-like connection.
Note: Preview mode when using Socket.IO seems to always use a non-websocket mode.
B
73
S
10
G
6
Posts: 105
Reputation: 7,577

Post » Sat Dec 07, 2013 9:54 pm

[QUOTE=Index]Socket.IO brings in extra functionality. It uses websockets but also has things like rooms(channels) and fallback policies*. A reason to not use Websockets is that if you don't need that functionality or you want to custom add what you want.

*Fallback policies are presented when a client fails to utilize websockets. This can allow non-websocket accepting browsers and mobile devices to still have a websocket-like connection.
Note: Preview mode when using Socket.IO seems to always use a non-websocket mode.[/QUOTE] I was going to use Socket.IO, but the error I posted above, debug - destroying non-socket.io upgrade, prevented me from testing it out because the server flat out refused to connect.

If you can get a Socket.IO server and C2 to cooperate that would be great. I can't. XD

Otherwise, I'm using WebSockets, mostly because it's the only vanilla support C2 has for creating multiplayer.

On the server side, though, I've taken a peek at Photon. I'm just not sure which would be better for a beginner with $0 money to spend, heh.
ImageImageImageImage
B
62
S
19
G
51
Posts: 633
Reputation: 30,826

Next

Return to Construct 2 General

Who is online

Users browsing this forum: bobcgausa and 10 guests