Search Unity

[Solved] Lidgren works on *.EXE but not in web browser ?

Discussion in 'Multiplayer' started by Arkdurath, Feb 5, 2010.

  1. Arkdurath

    Arkdurath

    Joined:
    Jan 10, 2010
    Posts:
    64
    Hi all

    Im developing an online web based game. Now i am implementing net code and i am using lidgren lib to handle net connections.

    If i make an executable with unity to run over windows, all works fine, but if compile all the project to be a web browser application, this application can't connecto to the server.

    I dont know why this is happening, i revised the project and lidgren dll is placed in his site.

    Any ideas ?

    Thanks for your time :)
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Is the DLL a .NET/Mono DLL or native code? Native code added in via the plugin system won't work in a web player due to security restrictions.
     
  3. Arkdurath

    Arkdurath

    Joined:
    Jan 10, 2010
    Posts:
    64
    Hi

    Thanks for your fast response.

    How can i check it ? Its my first time working with .Net and web based aplications :)

    Thanks for your time :)
     
  4. joew

    joew

    Joined:
    Apr 8, 2008
    Posts:
    96
    Just FYI the Lidgren library is completely managed C# code and the DLL would be a .Net assembly.
     
  5. Arkdurath

    Arkdurath

    Joined:
    Jan 10, 2010
    Posts:
    64
    Hi

    I think all Lidgren project is c#, so i think DLL will be .Net

    But anyway, i dont know why over web-browser it doesnt work. I tested on unity editor, and with copiled EXE and works perfectly.

    If loading of a DLL fails on web-browser, unity's program let any log at any part ? Is there any output to see whats is happening and why it doesent work ?

    Thanks for your time :)
     
  6. joew

    joew

    Joined:
    Apr 8, 2008
    Posts:
    96
    Have you tried using debug print statements to see what exactly is not being executed?
     
  7. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    check if lidgren relies on System.IO, System.Web Services or another removed class
     
  8. Arkdurath

    Arkdurath

    Joined:
    Jan 10, 2010
    Posts:
    64
    Hi

    @joew, How can i print debug info while aplication runs over web browser ?

    @dreamora, Do you mean that there are some parts of code that works on *.exe and not works on a web based application ?

    I will try to check these things, but i need first how to output decent information to know what is 'not working' over web browser.

    Thanks :)
     
  9. Arkdurath

    Arkdurath

    Joined:
    Jan 10, 2010
    Posts:
    64
    Hi again.

    Another user of Unity3D have the same problem as me. Lidgren server and Lidgren web-browser application can't connect. He send an e-mail to Lidgren creatos and ask him if web components allow to open UDP ports and listening ports.

    Anyone knows if web components have privileges to make this ? I remember that Photon works well over web-browser, and will be very sad and frustrating if Lidgren doesn't works over web-browser :(

    As said, anyone knows about UDP question ?

    Thanks a lot for your time.
     
  10. TrickyFishy

    TrickyFishy

    Joined:
    Aug 27, 2009
    Posts:
    39
    [NOTE: I posted the wrong build here so I removed the original attachment. Didn't re-attach it since Lidgren integrated the fixes into the SVN tree]


    I am using Lidgren for a web game and it appears to be working fine for me.

    BUT - you have to modify the Lidgren project because it uses one or two classes not available in a web deployment. I put a preprocessor block around it and #define UNITY3D to use my custom class. I know for one, the Lidgren NetTime class uses System.Diagnostics.Stopwatch which is not supported because it makes calls into QueryPerformanceCounters etc.

    You can probably see the exception being thrown if you look in your web player logs located in %temp%\UnityWebPlayer\log

    I've attached the build of Lidgren I'm currently using. Once I've got it all tested I'll get the source code released or submitted back to Michael (Lidgren)
     
  11. maroonedione

    maroonedione

    Joined:
    Nov 9, 2009
    Posts:
    1
    Thanks a lot.
     
  12. Arkdurath

    Arkdurath

    Joined:
    Jan 10, 2010
    Posts:
    64
    Hi TrickyFishy

    I am testing now what you said in your last comment.
    I found this in the webplayer log:

    Code (csharp):
    1.  
    2. -----------------------------------------------------------------------------
    3.  UNSECURED:
    4. System.Environment::get_MachineName()
    5.  
    6.  
    7. TypeInitializationException: An exception was thrown by the type initializer for System.Diagnostics.Stopwatch
    8. Lidgren.Network.NetBaseStatistics.Reset ()
    9. Lidgren.Network.NetBaseStatistics..ctor ()
    10. Lidgren.Network.NetBase..ctor (Lidgren.Network.NetConfiguration config)
    11. Lidgren.Network.NetClient..ctor (Lidgren.Network.NetConfiguration config)
    12. System.Diagnostics.Stopwatch..cctor ()
    13.  
    + Edit: Added this log too
    Code (csharp):
    1.  
    2.  
    3.  UNSECURED:
    4. System.Environment::GetCommandLineArgs()
    5.  
    6.  
    7. unity security internal call - throwing exception
    8. SecurityException: This method is not supported in the unity web player for security reasons.
    9. Lidgren.Network.NetClient..ctor (Lidgren.Network.NetConfiguration config)
    10. System.Environment.get_CommandLine ()
    11. Lidgren.Network.NetRandom..ctor ()
    12. Lidgren.Network.NetRandom..cctor ()
    13.  
    It seems that Stopwatch is not secure to be executed on webplayer, i dont know why but it says it.

    Anyway, i downloaded your DLL and copy on my project where i had mine, and this still doesn't work. Need i to setup something else ?

    I will send another mail to Michael Lidgren to advise him about this forum thread, maybe with all the help we could let Lidgren Lib work over webplayer :)[/code]
     
  13. lidgren

    lidgren

    Joined:
    Feb 8, 2010
    Posts:
    4
    I've checked in revision 227 which should fix the problems above if you sync and uncomment the line "//#define WEB_PLAYER" in NetTime.cs

    I'm not able to test these changes tho, so please write back and tell me if it's working :)
     
  14. Arkdurath

    Arkdurath

    Joined:
    Jan 10, 2010
    Posts:
    64
    Hi

    I will check it now :)

    Thanks for your work and time :)

    LLORENS
     
  15. Arkdurath

    Arkdurath

    Joined:
    Jan 10, 2010
    Posts:
    64
    Hi all

    I sync with svn to 227 revision. Uncomented the line and now Lidgren is fully working via web player :)

    Thanks a lot Michael for taking time to help us :) I will continue developing with Lidgren Lib.

    Thanks again :)

    LLORENS
     
  16. TrickyFishy

    TrickyFishy

    Joined:
    Aug 27, 2009
    Posts:
    39
    I'm glad you got it working! Ya I think I posted the incorrect build without the Unity fixes but it looks like we got it built into the main tree!

    Awesome.
     
  17. Daniel FF

    Daniel FF

    Joined:
    Mar 23, 2009
    Posts:
    70
    Hi,

    We can use lidgren with indie version ? if yes how to ? just import dll as a asset ?

    Thank you!!!
     
  18. Arkdurath

    Arkdurath

    Joined:
    Jan 10, 2010
    Posts:
    64
    Hi


    Yes, you need to download Lidgren network library.

    For the client, you need to create a folder in the same level of Assets folder, put into it the Lidgren.dll and create a component on Unity3D who creates a Lidgren Client object.

    For server, you can create a new console project, add Lidgren project to it and add reference to this project too. And of course, create a Server objetc and start sending packets to clients.

    If you want Lidgren to work over web browser, you need to acces via svn to Lidgren projects repository and download at last revision 227 of the project. Then find line with 'WEB_PLAYER' and uncoment it. And Lidgren is ready to work.

    See you

    LLORENS
     
  19. Daniel FF

    Daniel FF

    Joined:
    Mar 23, 2009
    Posts:
    70
    Thank you very much!! I will try that!
     
  20. r4ccoon

    r4ccoon

    Joined:
    Jun 25, 2010
    Posts:
    2
    hei, how have you been with the project?
    i am doing the same thing..
    could you post your postmortem or such?
    how do you deal with the network message loop.
    and how you deal with server side authoritative?
     
  21. Arkdurath

    Arkdurath

    Joined:
    Jan 10, 2010
    Posts:
    64
    Hi

    The project is fine, near to his finish phase. And Lidgren Lib is very stable. It's a good choise for me :)

    Authoritative protocol is very simple, login + password on a hail message, and then server accepts you or not. Next messages are sent without encryptation because Lidgren Lib author says he will implement this feature on the next Lib iteration (v 3.0).

    About message loop, i have a loop that reads all incoming messages, then, a big switch operates over them by the ID of the message protocol.

    And the protocol is a very simple, efficient and scalable protocol. Every message is a simple order and the receiver executes this order. Only the messages that contains players positions can be lost, all other messages are sent via Reliable channels.

    Hopes it help you :)

    LLORENS
     
  22. mVlipka

    mVlipka

    Joined:
    Jun 2, 2010
    Posts:
    42
    Hey, I have a quick question for you:
    Did Lidgren ever crash Unity on you?

    Every time I add the reference, Unity crashes on Client.Start();
    Did you encounter this problem?
     
  23. Arkdurath

    Arkdurath

    Joined:
    Jan 10, 2010
    Posts:
    64
    No sorry, Lidgren never crashes for me. Only crashes are generated by me reading more data from messages than they have inside.

    If you have problems with Lidgren, try to contact his author, its a very nice person and respond very quickly.

    Hopes it helps you.

    LLORENS
     
  24. mVlipka

    mVlipka

    Joined:
    Jun 2, 2010
    Posts:
    42
    Yeah, I contacted him, but he said there was nothing he could do.

    Do you call Client.Start() in Awake or Start? (Maybe calling it in Awake was my problem...).
     
  25. Arkdurath

    Arkdurath

    Joined:
    Jan 10, 2010
    Posts:
    64
    I need to confirm it because i dont have the code here, but i think it's on Start().

    I will try to write you on MOnday (if i remember it :)) about where is this function called.

    Hopes it help you :)

    LLORENS
     
  26. Arkdurath

    Arkdurath

    Joined:
    Jan 10, 2010
    Posts:
    64
    Hi again.

    This is my Start() function at NetHandler class wich maintain the net loop on Update() function.

    Code (csharp):
    1.  
    2.  
    3.     void Start()
    4.     {
    5.         NetConfiguration config = new NetConfiguration("myAppName"); // needs to be same on client and server!
    6.         m_Client = new NetClient(config);
    7.         m_Client.SetMessageTypeEnabled(NetMessageType.ConnectionRejected, true);
    8.  
    9.         // make hail
    10.         //NetBuffer TmpBuffer = m_Client.CreateBuffer();
    11.         //TmpBuffer.Write(Environment.MachineName);
    12.         //TmpBuffer.Write("Llorens" + UnityEngine.Random.Range(0,1000).ToString());
    13.         //TmpBuffer.Write("password");
    14. }
    15.  
    I dont know if it helps you or not, say something here if you have more problems

    LLORENS
     
  27. mVlipka

    mVlipka

    Joined:
    Jun 2, 2010
    Posts:
    42
    Thanks for posting your code.

    You never called Client.Start though or use Client.Connect.

    Maybe it crashed because I never set the message type... But I don't recall doing that on my other game's client.

    I'll look into this more later today probably.
     
  28. Arkdurath

    Arkdurath

    Joined:
    Jan 10, 2010
    Posts:
    64
    Mi old Update() function, its very old but it works

    Code (csharp):
    1.  
    2.     void Update()
    3.     {
    4.         // check if any messages has been received
    5.         while (m_Client.ReadMessage(buffer, out m_Type))
    6.         {
    7.             switch (m_Type)
    8.             {
    9.                 case NetMessageType.ServerDiscovered:
    10.                     // just connect to any server found!
    11.                     break;
    12.                 case NetMessageType.ConnectionRejected:
    13.                     Debug.Log("Rejected: " + buffer.ReadString());
    14.                     break;
    15.                 case NetMessageType.DebugMessage:
    16.                 case NetMessageType.VerboseDebugMessage:
    17.                     Debug.Log(buffer.ReadString());
    18.                     break;
    19.                 case NetMessageType.StatusChanged:
    20.                     string statusMessage = buffer.ReadString();
    21.                     Debug.Log("StatusMessage :" + statusMessage);
    22.                     NetConnectionStatus newStatus = (NetConnectionStatus)buffer.ReadByte();
    23.                     Debug.Log("New status: " + newStatus + " (" + statusMessage + ")");
    24.                     m_Status = newStatus;
    25.                     //m_List.Add(newStatus.ToString());
    26.                     break;
    27.                 case NetMessageType.Data:
    28.  
    29.                     ProcessData(buffer);
    30.                     break;
    31.             }
    32.         }
    33.  
    34.         if (m_Status == NetConnectionStatus.Connected)
    35.         {
    36.             m_fTimeSendPosCache -= Time.deltaTime;
    37.             m_fTimeSendChatCache -= Time.deltaTime;
    38.  
    39.             if (m_fTimeSendPosCache < 0.0f)
    40.             {
    41.                 SendPositionInfo();
    42.                 SendNpcSelection();
    43.                 SendNpcDeselection();
    44.                 m_fTimeSendPosCache = m_fTimeSendPos;
    45.             }
    46.  
    47.             if (m_fTimeSendChatCache < 0.0f)
    48.             {
    49.                 SendChatText();
    50.                 m_fTimeSendChatCache = m_fTimeSendChat;
    51.             }
    52.         }
    53.     }
    54.  
    And my OnGUI() function, that hold the prompt and 'connect' button to start the connection.

    Code (csharp):
    1.  
    2. void OnGUI()
    3.     {
    4.         if (m_Status == NetConnectionStatus.Disconnected)
    5.         {
    6.             GUI.Box(new Rect(m_vConnectWindow.x, m_vConnectWindow.y, 200, 140), "");
    7.             GUI.Label(new Rect(m_vConnectWindow.x + 5, m_vConnectWindow.y + 5, 50, 20), "Login:");
    8.             m_sLogin = GUI.TextField(new Rect(m_vConnectWindow.x + m_fFieldPosX + 5, m_vConnectWindow.y + 5, 100, 20), m_sLogin, 8);
    9.             GUI.Label(new Rect(m_vConnectWindow.x + 5, m_vConnectWindow.y + 25, 50, 20), "Password:");
    10.             m_sPassword = GUI.TextField(new Rect(m_vConnectWindow.x + m_fFieldPosX + 5, m_vConnectWindow.y + 25, 100, 20), m_sPassword, 8);
    11.  
    12.             GUI.Label(new Rect(m_vConnectWindow.x + 5, m_vConnectWindow.y + 50, 50, 20), "Server:");
    13.             m_sServerName = GUI.TextField(new Rect(m_vConnectWindow.x + m_fFieldPosX + 5, m_vConnectWindow.y + 50, 100, 20), m_sServerName);
    14.             GUI.Label(new Rect(m_vConnectWindow.x + 5, m_vConnectWindow.y + 70, 50, 20), "Port:");
    15.             m_sServerPort = GUI.TextField(new Rect(m_vConnectWindow.x + m_fFieldPosX + 5, m_vConnectWindow.y + 70, 50, 20), m_sServerPort);
    16.  
    17.             if (GUI.Button(new Rect(m_vConnectWindow.x + 60, m_vConnectWindow.y + 105, 80, 20), "Connect"))
    18.             {
    19.                 if (m_sLogin.Length > 0)
    20.                 {
    21.                     NetBuffer TmpBuffer = m_Client.CreateBuffer();
    22.                     TmpBuffer.Write(m_sLogin);
    23.                     TmpBuffer.Write(m_sPassword);
    24.                     m_Client.Connect(m_sServerName, int.Parse(m_sServerPort), TmpBuffer.ToArray());
    25.                 }
    26.             }
    27.         }
    28.     }
    29.  
    Hopes it help you

    LLORENS
     
  29. David Bennell

    David Bennell

    Joined:
    Apr 25, 2010
    Posts:
    5
    Yeah this happens on the new Gen3 Lidgren library
    calling client.Start() causes unity to bomb out

    The old one works but I can only find an old libary from 2007 so I don't have a more up-to-date version as he seems to have removed everything but the Gen3 version from his site.

    I am still trying to identify what exactly is causing the problem with the new library.
     
  30. Arkdurath

    Arkdurath

    Joined:
    Jan 10, 2010
    Posts:
    64
    Hi

    Each generation of lidgren has his own google page, so try to fund gen2 page and download it, i think its still there.

    See you

    LLORENS
     
  31. David Bennell

    David Bennell

    Joined:
    Apr 25, 2010
    Posts:
    5
    Arkdurath - ah yes your right, I have found the all three sites now.

    Yes the last one I can find that works with unity is the gen2 release on 2010-01-22 if anyone else is also trying to get this working.
     
  32. Arkdurath

    Arkdurath

    Joined:
    Jan 10, 2010
    Posts:
    64
    Did you report to Michael Lidgren about Gen3 crash on Unity ?

    There are interesting features in Gen3. It will be very sad we cannot us them in future :S

    LLORENS

    Edit: There are a google.group about Lidgren, you can post there about it :)
     
  33. mVlipka

    mVlipka

    Joined:
    Jun 2, 2010
    Posts:
    42
    I actually reported it to him, he said he cannot fix sense it is a Unity issue.

    I'm assuming it is because Unity uses a very old Mono version (I compiled it with Mono 1.24 and it had a HUGE amount of problems, a lot had to do with the use of "var").

    Once Unity 3.0 comes out, it should work fine.
     
  34. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
    Tbqh, the only bug report I saw in his profile supplied no useful information.

    http://code.google.com/p/lidgren-network-gen3/updates/list

    I actually laughed when I read it.
     
  35. mVlipka

    mVlipka

    Joined:
    Jun 2, 2010
    Posts:
    42
    And what would you have put?

    Describe to me what Client.Start() is supposed to do. It is a built in function for Lidgren, he should know exactly what it does. Why should I waste my time to tell him what he already knows? That would be a waste of time, and rather stupid.
     
  36. IvanSugrobov

    IvanSugrobov

    Joined:
    Jul 16, 2010
    Posts:
    3
    Hi All!
    Comment this code in NetPeer.Internal.cs
    Code (csharp):
    1. System.Net.NetworkInformation.PhysicalAddress pa = NetUtility.GetMacAddress();
    2.             if (pa != null)
    3.             {
    4.                 m_macAddressBytes = pa.GetAddressBytes();
    5.                 LogVerbose("Mac address is " + NetUtility.ToHexString(m_macAddressBytes));
    6.             }
    7.             else
    8.             {
    9.                 LogWarning("Failed to get Mac address");
    10.             }
    and add definition of pa
    may be like this
    Code (csharp):
    1. object pa = null;
    comment this
    Code (csharp):
    1. #define IS_STOPWATCH_AVAILABLE
    in NetTime.cs
    and in NetRandom.cs comment Stopwatch smthng like this
    Code (csharp):
    1. seed ^= 10;// (int)(Stopwatch.GetTimestamp());
    Regards
    Ivan
     
  37. lidgren

    lidgren

    Joined:
    Feb 8, 2010
    Posts:
    4
  38. ColossalDuck

    ColossalDuck

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    Nice, thanks Lidgren.
     
  39. runningbird

    runningbird

    Joined:
    Sep 3, 2009
    Posts:
    382
    Awesome...thanks Lidgren I will switch to Gen 3 tonight.
     
  40. runningbird

    runningbird

    Joined:
    Sep 3, 2009
    Posts:
    382
    Did what the others said to do for Gen 3 and it's working perfectly with the web player.

    Love the newer version much easier to implement etc.
     
  41. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    I confirm that gen3 (revision 124) doesn't work in browser, but today I got another error. After I changed some lines needed for Unity I get an exception in Unity (not a silent fail anymore).

    Danko
     

    Attached Files:

    • $03.png
      $03.png
      File size:
      17.5 KB
      Views:
      709
    • $04.png
      $04.png
      File size:
      19.2 KB
      Views:
      761
  42. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    I found what was causing this null reference.

    The line below "// random bytes is better than nothing" (in NetPeer.Internal.cs) has to be commented out because "m_macAddressBytes" is not defined.

    In this case "nothing" seems to be better. :)

    However it didn't solve my player problem (works from editor and windows app, seems not to be the crossdomain policy issue).
     

    Attached Files:

    Last edited: Oct 11, 2010
  43. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
  44. lidgren

    lidgren

    Joined:
    Feb 8, 2010
    Posts:
    4
    Oops; the defines were a little untested. Should work as of revision 127 tho.
     
  45. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    the non working comes from the lack of policy server capability.
    you need to fetch the policy from the server before connecting to it so it will work on the webplayer.

    see the sandbox and unity 3 webplayer entry in the manual
     
unityunity