Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

websocket-sharp on Unity

Discussion in 'Scripting' started by shinriyo_twitter, Nov 7, 2011.

  1. shinriyo_twitter

    shinriyo_twitter

    Joined:
    Aug 12, 2011
    Posts:
    328
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    if you try to use that on the webplayer it will definitely not work.

    On standalones it should work.

    what does the editor.log mention on the error?
     
  3. shinriyo_twitter

    shinriyo_twitter

    Joined:
    Aug 12, 2011
    Posts:
    328
    Thanks for reply.
    Later, when I back to home I will write what I modify.
     
  4. shinriyo_twitter

    shinriyo_twitter

    Joined:
    Aug 12, 2011
    Posts:
    328
    Hi dreamora

    I did that

    1. I created Plugins directory on Unity.

    I download
    git clone https://github.com/sta/websocket-sharp.git

    I put websocket-sharp directory of websocket-sharp to the Plugin directory,

    2. I deleted Ext.cs file.

    3. I commented out name spaces in all files
    Code (csharp):
    1.  
    2. //namespace WebSocketSharp
    3. //{
    4.     // something
    5. //}
    6.  
    4. I fixed WebSocket.cs
    Code (csharp):
    1.  
    2. //      string secKey1 = rand.GenerateSecKey(out key1);
    3. //      string secKey2 = rand.GenerateSecKey(out key2);
    4. int secKey1 = rand.Next(0, 1000);
    5. key1 = (uint)secKey1;
    6. int secKey2 = rand.Next(0, 1000);
    7. key2 = (uint)secKey2;
    8.        
    9. //      byte[] key3 = new byte[8].InitializeWithPrintableASCII(rand);
    10.       byte[] key3 = new byte[8];
    11.  
    GenerateSecKey and InitializeWithPrintableASCII are not found.
    I guess I mistook then.

    5. I commented out Exception procedure
    Code (csharp):
    1.  
    2.       /*
    3.       string msg;
    4.       if (!(response.IsValid(expectedCR, actualCR, out msg)))
    5.       {
    6.         throw new IOException(msg);
    7.       }
    8.       */
    9.  
    6. I fix there cuz I couldn't understand
    Code (csharp):
    1.  
    2. //        if (wsStream.ReadByte().EqualsWithSaveTo('\r', rawdata)
    3. //            wsStream.ReadByte().EqualsWithSaveTo('\n', rawdata)
    4. //            wsStream.ReadByte().EqualsWithSaveTo('\r', rawdata)
    5. //            wsStream.ReadByte().EqualsWithSaveTo('\n', rawdata))
    6.         if (wsStream == rawdata.ToArray()) {
    7.  

    7. I created nodejs file "server.js" and started up via command "node server.js"
    Code (csharp):
    1.  
    2.  
    3. equire('http').createServer(function (request, response) {
    4.   response.writeHead(200, {"Content-Type": "text/plain"});
    5.   output = "Hello World!\n";
    6.   for (k in request.headers) {
    7.     output += k + '=' + request.headers[k] + '\n';
    8.   }
    9.   response.end(output);
    10. }).listen(8080);
    11.  
    8. I created Server.cs on Unity
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Server : MonoBehaviour {
    6.    
    7.     void Awake () {
    8.         WebSocket ws = new WebSocket("ws://127.0.0.1:8080/");
    9.         ws.OnOpen += (o, e) => {
    10.             Debug.Log("Open");
    11.         };
    12.         ws.Connect();
    13.     }
    14. }
    15.  
    9. But Unity was freeze without error....
    Why?
     
  5. shinriyo_twitter

    shinriyo_twitter

    Joined:
    Aug 12, 2011
    Posts:
    328
    I reopened, Unity didn't freeze

    but, in spite of i wrote Debug.Log("Open");, but Open didn't apeered
     
  6. scarpelius

    scarpelius

    Joined:
    Aug 19, 2007
    Posts:
    966
    I believe you should compile websocket-sharp project and generate the .dlls to be able to use them.
     
  7. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Would go that way too, just drop it in as managed dll.
    that way you can also much easier update them.

    Just ensure that you compile the dll as .NET 2.0 - .NET 3.5 only, not 4.0 as thats not going to work at all
     
  8. shinriyo_twitter

    shinriyo_twitter

    Joined:
    Aug 12, 2011
    Posts:
    328
    Hello
    Is creating " managed dll" enable on MonoDevelop?

    I did below

    On the window menu of MonoDevelop
    File => New => Solution...

    then "New Solution" window was opened

    then, I select C# => Library => fill "websocket-sharp" on "Name" blank => "Forward" button

    "Project Features" window -> Packaging -> OK


    but, DLL didn't create

    I didn't find ".NET 2.0 - .NET 3.5 " selection.
    when updating websocket-sharp is only create dll?
     
  9. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    no need to create anything new, it has a solution in that you can just open.

    the .net 2 stuff is in the prjoect configuration when it comes to what .net version to use
     
  10. shinriyo_twitter

    shinriyo_twitter

    Joined:
    Aug 12, 2011
    Posts:
    328
  11. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    that is one option but what I meant is if you clone the whole thing you linked in in the first posting there is the solution file. doubleclick it an you are in mono and can compile the dll etc. thats how you normally work in C#

    if you use the websocket-sharp.dll you just drop it into Asset/Plugins and are fine.


    seems like you might want to learn more about general C# development in 'real world' outside of unity at least if you assume that you will need to understand what it does or create further dlls, so you are not getting lost.
     
  12. shinriyo_twitter

    shinriyo_twitter

    Joined:
    Aug 12, 2011
    Posts:
    328
    I just droped websocket-sharp.dll into Asset/Plugins.

    192.168.7.75:8124

    Code (csharp):
    1.  
    2. var http = require('http');
    3.  
    4. http.createServer(function (request, response) {
    5.   response.writeHead(200, {'Content-Type': 'text/plain'});
    6.   response.end('Hello World\n');
    7. }).listen(8124);
    8.  
    9. console.log('Server running at http://127.0.0.1:8124/');
    10.  
    it is server's helloworld.js

    and I started
    Code (csharp):
    1.  
    2. node helloworld.js
    3.  
    I can access "192.168.7.75:8124" via web browswe chrome

    It is Unity code
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using WebSocketSharp;
    5.  
    6. public class Server : MonoBehaviour {
    7.    
    8.     void Awake () {
    9.         WebSocket ws = new WebSocket("ws://192.168.7.75:8124/");
    10.         ws.OnOpen += (o, e) => {
    11.             Debug.Log("Open");
    12.         };
    13.         ws.Connect();
    14.     }
    15. }
    16.  
    and I attached it to Main Camera


    Error occurd.


    SocketException: Connection refused
    System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP, Boolean requireSocketPolicy)
    System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP)
    System.Net.Sockets.TcpClient.Connect (System.Net.IPEndPoint remote_end_point)
    System.Net.Sockets.TcpClient.Connect (System.Net.IPAddress[] ipAddresses, Int32 port)

    I hope Open log!
     
  13. ajpessoa

    ajpessoa

    Joined:
    Dec 11, 2013
    Posts:
    3
    This is an old thread but I'm currently having the same problem. I've seen posts on github of people saying they got it working on the webplayer but it always gives me the same problem:

    WebSocket.acceptException|System.Security.SecurityException: Unable to connect, as no valid crossdomain policy was found

    I've tried to use the Security.PrefetchSocketPolicy line and I have the crossdomain.xml allowing socket connection to any port (*) and it still won't work with the webplayer, but it does work with a standalone build and on the editor.

    I must be doing something wrong but I can't find out what, so I could use some help