Search Unity

  1. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice
  2. Unity 2017.3 has arrived! Read about it here.
    Dismiss Notice
  3. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Simple Unity Network Host For a Python Client?

Discussion in 'Multiplayer Networking' started by KrisSchnee, Jul 10, 2014.

  1. KrisSchnee

    KrisSchnee

    Joined:
    Mar 9, 2014
    Posts:
    14
    Years ago I built a simple testbed for an AI project, using Python, Pygame, and sockets. It acts as a server and can accept socket-based connections on a local machine to send simple text I/O like "go e" or "you see object #42 @x,y". That's all I need from the connection: log in, log out, send regular text messages about a tile-based, turn-based grid world. At most, say, four simultaneous connections but usually one.

    I'd like to make a version of the testbed for Unity, because of framerate, easy zooming, and other features. The trick is that I want to talk to this server testbed using an external Python program, not Unity, so that I can do my own AI code in a language I'm most comfortable with. It's not even a game project, really, just seeing what I can do with some AI ideas against the most functional (if not the prettiest) environment I can build and open-source.

    My first foray into doing a Unity server hasn't worked. The closest I've gotten has been with Websocket-Sharp ( https://github.com/sta/websocket-sharp ), which got me to the point of having the line "wssv.isListening" or something reporting true, indicating an open connection, and my Python program not reporting an error when told to open a socket to that port. Couldn't listen though, didn't see an open port for that in a Windows console, and couldn't get Unity to receive messages.

    Any thoughts on this project?
     
  2. mmehrten

    mmehrten

    Joined:
    Jul 15, 2016
    Posts:
    1
    Hey Kris, I assume this is pretty late to the game, but since Python is going to be the main controller for this AI have you considered making Python the server and Unity the client? Then you can just send messages from python to unity as needed with the information that you need. If it's of any use, I'm writing a tutorial to do just that. You can check out the tutorial and the GitHub here.
     
  3. MistyAcid

    MistyAcid

    Joined:
    Jul 18, 2016
    Posts:
    1
    Where could we check out said tutorial?
     
  4. grimunk

    grimunk

    Joined:
    Oct 3, 2014
    Posts:
    202
    A websocket and socket are not the same thing. A socket is just an open descriptor. If you open it as a TCP socket (SOCK_STREAM), you can just read/write from it as though it is a file. Websockets is something you do on top of HTTP, which is itself a more complicated protocol built on top of TCP.

    Don't use Unity websockets. You can just use standard c sharp sockets - they are available.