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

  • 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. :P

  • 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.

  • 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!

  • 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:

    <font color="red">socket.on('news', function (data) {

        console.log(data);

        socket.emit('my other event', { my: 'data' });</font>

    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:

    <font color="red">socket.on('moveChar', function (data) {

        console.log(data);

    });</font>

    Instead of using something like:

    <font color="red">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.

  • 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.

  • 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.

  • 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.

  • 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. :)

  • 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.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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.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.

  • Add

    io.disable('destroy upgrade');

    [io is related to the "require('socket.io')" statement]

  • Thanks, :)

    So does that just mean that Socket.IO cuts connections by default unless the client is also using Socket.IO?

  • I suppose so. Which makes me wonder why previewing makes it think you're not using Socket.IO.

  • Bringing this back up, it appears that Socket.IO flat out refuses anything Construct 2 can throw at it.

    I've made the most simplistic server I could possibly manage, even disabling the retarded "destroy upgrade:"

    <font color=black>var io = require('socket.io').listen(8000);

    io.configure(function(){io.disable('destroy upgrade'); console.log('configured');});

    io.sockets.on('connection', function (ioC) {

         console.log('connection opened');

    });</font>

    This successfully starts the server, and the string "configured" is logged as expected. However, no connection is made. At all.

    I've taken a look at dozens of user snippets and there are no diversions from other code. It's just boggling my mind.

    With my client I can connect perfectly fine to the WS module server I set up, sending packets like mad, but Socket.IO just fails.

    Any help? Suggestions? I feel only like I've wasted two hours trying to get it to work, with 0 results.

    @Index, Ashley

  • If you have decided to definitely go with socket.io on the server side, you should also use socket.io on the client side. I guess they implement some kind of protocol on top of websockets so you'll need both ends to be speaking the same dialect.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)