Search Unity

Lost in socketing: Unity with node.js?

Discussion in 'Connected Games' started by SV_Janjo, Jun 11, 2014.

  1. SV_Janjo

    SV_Janjo

    Joined:
    Sep 3, 2013
    Posts:
    8
    Hi guys

    I'm quite new to socketing and all the asynchronous client-server stuff. I'm making web game that uses webplayer to display battle arena. Until now, I used http class to communicate with the server but now I would like to switch to socket communication. But I don't know where to begin.

    As a server I choose node.js (sure, you can reccomend better solutions but only with understandable tutorials) with websocket extension. On Unity side, i use websocket-sharp library. I found very weak support, tuturials or examples for this library. First I run the server script. On game start, I connect to server on localhost (address ws://127.0.0.1:13337/ - that's the adress and the port i'm using in node.js) but when I run the game, nothing happens (no error, no connection found on server). How do I debug this? How do I check whether client is connected?

    As I said, I feel completely lost now. Could you offer me suitable tutorials, libraries or whatever? I need to find:
    1. Server technology
    2. Client library
    for my web game to use sockets. Help me, thanks!
     
  2. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,041
    My recommendation would be to use C# for the networking server also, so you can share code between your client and server.
     
  3. SV_Janjo

    SV_Janjo

    Joined:
    Sep 3, 2013
    Posts:
    8
    All right, but it wasn't very helpful for me as beginner in this area. I need more guidance and examples. But thank you anyway.
     
  4. SV_Janjo

    SV_Janjo

    Joined:
    Sep 3, 2013
    Posts:
    8
    OK, I keep struggling but there is little progress:

    1. I switched to SharpConnect library.
    2. I created and run server on local port 843 to send crossdomain policy xml, here's the code:
      Code (JavaScript):
      1. var net = require("net");
      2. var server = net.createServer(function(c) {
      3.  
      4.     c.on('error', function(error) {
      5.         console.log(error.toString());
      6.     });
      7.  
      8.     c.on('data', function(data) {
      9.  
      10.         var d = data.toString();
      11.  
      12.         if (d == '<policy-file-request/>\0') {
      13.  
      14.             var xml = "<?xml version=\"1.0\"?>\n"
      15.                 + "<cross-domain-policy>\n"
      16.                 + "<allow-access-from domain=\"*\" to-ports=\"13330-13337\"/>\n"
      17.                 + "</cross-domain-policy>\n";
      18.  
      19.             console.log(xml);
      20.             c.write(xml);
      21.  
      22.             c.end();
      23.         }
      24.  
      25.        
      26.     });
      27.  
      28. }).listen(843);
    3. And then I created and run gameserver itself on port 13337.
    4. Within Unity I connect like this:
      Code (JavaScript):
      1. if (Security.PrefetchSocketPolicy("127.0.0.1", 843, 10000)) {
      2.         Debug.Log('policy loaded');
      3.     } else {
      4.         Debug.Log('policy not loaded');
      5.     }
      6.  
      7.     test = new Connector();
      8.  
      9.     Debug.Log(test.fnConnectResult("127.0.0.1", 13337, System.Environment.MachineName));
    As log says (and policy server console), game successfuly connects to policy server to prefetch SocketPolicy xml. But the last line throws following error:
    System.Security.SecurityException: Unable to connect, as no valid crossdomain policy was found
    Why is that? The security was successfully prefetched... or am I doing something wrong? Please check my code and help me, thanks :)
     
  5. SV_Janjo

    SV_Janjo

    Joined:
    Sep 3, 2013
    Posts:
    8
    Nobody can help me?

    My progress: I decided to run sockpol.exe on server to send crossdomain policy file. Server sends following policy:
    Code (CSharp):
    1. <?xml version='1.0'?>
    2. <cross-domain-policy>
    3.     <allow-access-from domain="*" to-ports="*" />
    4. </cross-domain-policy>
    But when i then connect (Connection script in my previous post) to my GameServer, which is in node.js, following error appears:

    No connection could be made because the target machine actively refused it.

    I tried to turn off Firewall but it didn't help.

    This is node.js server running on port 13337:

    Code (JavaScript):
    1. var net = require('net');
    2.  
    3. var server = net.createServer(function(c) { //'connection' listener
    4.  
    5.       console.log('server connected');
    6.  
    7.     c.on('end', function() {
    8.         console.log('server disconnected');
    9.     });
    10.  
    11.     c.on('data', function(data) {
    12.  
    13.         var d = data.toString();
    14.         console.log(d);
    15.        
    16.     });
    17.  
    18.     c.on('error', function(error) {
    19.         console.log(error.toString());
    20.     });
    21.  
    22.     c.write('hello\r\n');
    23.  
    24.     c.pipe(c);
    25. });
    26.  
    27. server.listen(13337, '127.0.0.1', function() { //'listening' listener
    28.     console.log('server bound');
    29. });
    What can I do? Dont you have any example project where this works in webplayer?
     
  6. mgdevel83

    mgdevel83

    Joined:
    Oct 2, 2013
    Posts:
    7
    If you are using Unity free with iOS / Android?
     
  7. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,041
    Then you can use nativet sockets and p/invoke to them
     
  8. mgdevel83

    mgdevel83

    Joined:
    Oct 2, 2013
    Posts:
    7
    Not sure if its ok, but actually a relative question for @fholm, Is there any way to implement Web sockets with Unity Free. I do have socket.io based application. I found some Socket.IO client for unity (one using websocket-sharp). But looking at code since found they are using System.net package, so doubt if they will work with Unity free for iOS / Android and WP8.

    My only requirement is presence detection and sending event to game client when data is ready to pull. Polling is the only route otherwise, which isn't cool for mobile platforms.
     
  9. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,041
    Yes it is possible, you can call into native sockets and do it that way, requires specific C/C++ code for android and ios.
     
  10. mgdevel83

    mgdevel83

    Joined:
    Oct 2, 2013
    Posts:
    7
    How about performance? As I understand, native callbacks are performance heavy operations and specially when those are I/O related.

    If I can connect to Socket.IO service using Unity free on all these 3 (iOS, Android and WP8) platforms, then it will solve my current blocking issue. Is there any free plugin available, which can be used to try in my case. One which I found was for $75 which is big ask for me, specially if its just for trial.

    Creating own plugin is not possible for me at this time. So even if paid and available for all platform which allow to connect socket.io based server without any change at server end, I might consider that.
     
  11. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,041
    Native overhead is fine, it happens for the normal sockets anyways. You need to build native plugins for iOS and Android, and another custom solution for WP8 as it has a new socket platform
     
  12. mgdevel83

    mgdevel83

    Joined:
    Oct 2, 2013
    Posts:
    7
    Okay, thanks for help. One last thing, is there any tutorial for beginners for writing plugins for iOS and Android? I don't have experience with JNI / NDK and Objective-C.

    So what I can attempt is getting any open source socket.io client for Android / iOS and WP8 native and creating a wrapper for that to expose basic required method (like connect, disconnect, sendData, readData etc) to Unity in some way. At this point, given that never wrote a native plugin for Unity even this seems a big challenge to me. So if can get a template / step by step tutorial for basic plugin creation, that would be helpful.
     
  13. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,041
    There are no good native plugin tutorials, and you need a fair bit of C experience to pull it of, plus know how to handle all platform specific quirks on the native side
     
  14. mgdevel83

    mgdevel83

    Joined:
    Oct 2, 2013
    Posts:
    7
    So it means If I don't upgrade to Unity Pro, there aren't much options, unless I objective-c, java, c for plugin creation first. Too much for a beginner. Thanks a lot for the help.
     
  15. RichardKain

    RichardKain

    Joined:
    Oct 1, 2012
    Posts:
    1,247
    I ran into a similar issue myself. I checked the Unity Asset Store and found this.
    Good-Ol-Sockets
    It works. It's $25 dollars. You can write your own native plugins for the platform, or you can pony up $25 and save yourself a week's worth of headache. I tested it with my software last night and got basic UDP broadcasting and receiving working just fine on my Android phone. It gives you access to pretty much all of the Socketing and custom networking functionality you could want, and it features plugins for Android, iOS, and even Windows Phone.
     
  16. mgdevel83

    mgdevel83

    Joined:
    Oct 2, 2013
    Posts:
    7
    @RichardKain, if this works for WP8 ? In store description I couldn't locate WP8. And if this works with Node.js / Socket.io ?
     
  17. RichardKain

    RichardKain

    Joined:
    Oct 1, 2012
    Posts:
    1,247
    When I downloaded it, it came with a plugin for Windows Phone 8. Also, on the company website for Good-Ol-Sockets they mention that the latest upgrade added support for Windows Phone 8. I'm not certain if it would run with Node.js. But it effectively replicates the functionality of the System.Net package. This includes everything in System.Net.Sockets.

    It should be able to establish connections using standard C# socket commands. And I would imagine it would be able to communicate with software written using Node.js. Aside from that I can't really say. My own use of it is to allow for UDP broadcasting and receiving in order to make local network games using smartphones as clients.
     
  18. SV_Janjo

    SV_Janjo

    Joined:
    Sep 3, 2013
    Posts:
    8
    I moved a bit in this issue but still have big problems that I cannot solve and nobody around me is able to help me. Maybe you guys could...

    I used the policy server packed within unity installation and run it. I made my GameServer in nodejs but later tried server in C# as @fholm advised. In both cases the policy xml is preloaded correctly but connecting to GameServer itself throws the error: "System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it"

    Funny thing is that server is running and I can connect to it via telnet so it looks more like Unity problem. And it does the same thing with nodejs and C# too. Anyone has some guidance for me? I can send some sources if needed. Thank you.
     
  19. SV_Janjo

    SV_Janjo

    Joined:
    Sep 3, 2013
    Posts:
    8
    OK, I decided to track packets to see where they're getting lost. I noticed that whatever server port I set in sharpconnect library, packets are sent to port 10000. For now I decided to run server on this particular port and it seems to be working. Does anybody know why is Unity sending data to this port?
     
  20. Tomo-Games

    Tomo-Games

    Joined:
    Sep 20, 2010
    Posts:
    220
    SV_Janjo I wrote something about this a while back. Maybe it will be of some use.
     
  21. Sephiroth74

    Sephiroth74

    Joined:
    Dec 9, 2012
    Posts:
    20