Yet-another Multiplayer Demo (with capx)

0 favourites
From the Asset Store
Demo Game Multiplayer Online with member registration system
  • Hi, I have some issues with your modified Socket-IO plugin, the "Send" action and some conditions (such as "on connect") do not seem to work.

    I currently use a quick Python server like this :

    import socket
    import sys
    
    PORT = 9999
    
    handshake = '''
    HTTP/1.1 101 Web Socket Protocol Handshake\r\n\  
    Upgrade: WebSocket\r\n\  
    Connection: Upgrade\r\n\  
    WebSocket-Origin: http://localhost:50000\r\n\  
    WebSocket-Location: ws://localhost:'''+str(PORT)+'''/\r\n\r\n'''  
    handshakeDone = False
    data = "" 
    header = ""
    
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  
    sock.bind(("", PORT))  
    sock.listen(5)
    print("Listening on port", PORT)
    
    print("Accepting...")
    client, address = sock.accept()
    print("Client connected.")
    while True:  
        if not handshakeDone:  
            header += client.recv(16).decode()
            if header.find('\r\n\r\n') != -1:
                data = header.split('\r\n\r\n', 1)[1]  
                handshakeDone = True
                print("Handshake Done !")
                client.send(handshake.encode())
                print("Handshake sent back to client.")
        else:
                print("Waiting for data...")
                tmp = client.recv(128).decode()
                print("Data received : ",tmp)
                data += tmp;
    
                validated = []  
      
                msgs = data.split('\xff')  
                data = msgs.pop()  
      
                for msg in msgs:  
                    if msg[0] == '\x00':  
                        validated.append(msg[1:])  
      
                for v in validated:  
                    print(v)  
                    client.send('\x00' + v + '\xff'.encode())
    

    And some events like this (I verified names and conditions etc...)

    <img src="http://i.imgur.com/9Vlro.png" border="0" />

    I can successfully connect to my server (however, the "OnConnect" event does not trigger). But whenever I try to send data to my server, nothing happens on serverside (the socket is still receiving). And when I close the browser I test my layout in, the Python Console gets full of :

    Waiting for data...
    Data received :
    
    Waiting for data...
    Data received :
    
    Waiting for data...
    Data received :
    
    Waiting for data...
    Data received :
    
    Waiting for data...
    Data received :
    
    Waiting for data...
    Data received :
    
    etc...

    Any clues to get this problems solved ?

    Thanks.

  • Do you tried your project on an online server ?

    I've a small game working perfectly on local network, and i'm trying to put it online, on an ubuntu server (12.04) but i cant connect to the server ... (And ofc I've no error). Other simple node app works perfectly or at least i got the http:// response.

    Any idea ?

    Ty !

  • OrionPyro

    Python isn't my stronger side but here's my two cents:

    I can successfully connect to my server (however, the "OnConnect" event does not trigger). But whenever I try to send data to my server, nothing happens on serverside (the socket is still receiving).

    Not sure about this, sounds like some server side problem, but need more info. i really dont understand the socket is still receiving part.

    And when I close the browser I test my layout in, the Python Console gets full of :

    Waiting for data...
    Data received :
    
    Waiting for data...
    Data received :
    
    etc...

    Looks like you didn't handle what happens when a client disconnects, so your server stays in a loop (because of while True:), and goes to else part, prints "waiting for data", Data received : ",tmp,

    but tmp is empty.

    I think you should end while loop when a client disconnects.

  • Not sure about this, sounds like some server side problem, but need more info. i really dont understand the socket is still receiving part.ell, it means that the server-side socket is still waiting for data at this line :

    tmp = client.recv(128).decode()

    But I guess that the "Send" action does not work correctly, since the server-side socket can't receive anything after the handshake. Also, I tried with a regular Python client in order to test, it works well.

    Looks like you didn't handle what happens when a client disconnects, so your server stays in a loop (because of while True:), and goes to else part, prints "waiting for data", Data received : ",tmp,

    but tmp is empty.

    I think you should end while loop when a client disconnects.

    owever, socket is still in blocking-mode, whatever, it does not seem to be the main problem (I will add thing to manage disconnecting btw)

  • When i try simple node.js server everything works fine, so I don't believe that "Send" action does not work correctly.

    Also, when I try your server, i get same behavior as you, except when I close the browser I get the following message:

              

    UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)

    then i tried bunch of different examples i found on the web, the best one (which nicely prints all the messages received, but still doesen't work) is here:

    http://stackoverflow.com/questions/2153294/python-example-of-joes-erlang-websocket-example

    so, i believe your current server isn't doing the whole connection/handshake thing properly, and the client and server don't actually connect, although they exchange handshakes.

    (this is interesting read: https://github.com/learnboost/socket.io-spec)

    i would try this:

    http://autobahn.ws/python

    or this:

    https://github.com/Lawouach/WebSocket-for-Python

    or this:

    http://code.google.com/p/pywebsocket/

    and more info here:

    http://en.wikipedia.org/wiki/Comparison_of_WebSocket_implementations

  • When i try simple node.js server everything works fine, so I don't believe that "Send" action does not work correctly.

    Also, when I try your server, i get same behavior as you, except when I close the browser I get the following message:

              

    UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)

    then i tried bunch of different examples i found on the web, the best one (which nicely prints all the messages received, but still doesen't work) is here:

    http://stackoverflow.com/questions/2153294/python-example-of-joes-erlang-websocket-example

    so, i believe your current server isn't doing the whole connection/handshake thing properly, and the client and server don't actually connect, although they exchange handshakes.

    (this is interesting read: https://github.com/learnboost/socket.io-spec)

    i would try this:

    http://autobahn.ws/python

    or this:

    https://github.com/Lawouach/WebSocket-for-Python

    or this:

    http://code.google.com/p/pywebsocket/

    and more info here:

    http://en.wikipedia.org/wiki/Comparison_of_WebSocket_implementations

    I use Python 3.2, that's why I do not use these libraries.

    Anyway I'm going to try out this websocket example, and reading more about handshaking.

  • hi all,

    i dunno how do that:

    Array      set value at int(Socket.lastDataElement(1)) to Sprite.UID

    can anyone show me pleace?

    is in the picture of this fantastic tutorial thank's so much

  • sorry for the post i solved <img src="smileys/smiley1.gif" border="0" align="middle" />

  • hi i try this tut with my construct file a simple ship in a background but i dunno why when i connect my ship the count of ship go over 1000 he put on my background 1 ship every 0.07 second why?

  • LimonSpace : There is one event in the sample file that says "every 0.07", in the sample file it sends the position of the managed player to the server. What actions do you have there?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • sorry for not being able to answer before. Thank JohnnySheffield Juantar and for answering my post. I could not move on I want to do with the game for different reasons. but I still have the idea, I'm looking JohnnySheffield cravings what you're doing, in any event upon termination and complete what I want to do and get on some host I have seen some there, to visit it and give me your opinion .

  • LimonSpace : There is one event in the sample file that says "every 0.07", in the sample file it sends the position of the managed player to the server. What actions do you have there?

    i've the same your's.

  • Use the socket.on disconnect function to broadcast it.

    I see how to do it, but I have no idea what to enter in after that.

    example_server.js

    socket.on (disconnect) {

    ??

    }

  • First, when a player connects, server must give him his unique number, or a client must notify server what is his number, and store that number on server somehow (you can make your own list of numbers, or use a socket.id number or you can use constructs sprite ID, or etc. but clients and server must know what that number is).

    So, when connects, client can do something like:

    On connect

         Socket send message ("PLAYER, MYID") // in this case client notifys server what his number is

    Depending on what kind of arhitecture you want, you must code this part yourself, you can do something like this maybe (code is a mess, just for a general idea)

    myNumber = 0;

    var Me = [];

    io.sockets.on("connection", function (socket) {

         

        socket.on("message", function (data) {

            var new_data = data.split(',');

           

            if (new_data[0] == 'PLAYER') {

                  

              var myNumber = count++;    

              Me[0]='player'

              Me[1]=new_data[1];

    }

    socket.on('disconnect', function (data) {

              socket.broadcast.emit("message",'D,'+ Me[1]);

              //remove player from the list here somehow; Me[1]=null

                   }

    So, when a player disconnects, you can notify all connected games that a specific player is (D)ead, and what is his ID, so that client can:

    On Message "D"

           Pick instance(ID)

               Destroy

    sorry for the messy post, i'm in a hurry but that's how i'd do it. There is probably much more smarter way for this.

  • > Use the socket.on disconnect function to broadcast it.

    I see how to do it, but I have no idea what to enter in after that.

    example_server.js

    socket.on (disconnect) {

    ??

    }

    solved thanks

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