Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by KazYamof · Jan 20, 2016 at 06:38 PM · networkingwebglsocket

WebSocket and WebGL error

Hi there, I'm trying to use this free websocket pack with Unity. The demo works ok.

But now I need to do something more real. I'm trying to built a Server C# (console app) to run on Windows and make the Unity WebGL client to connect to it. My server code follows this example.

Server code:

 class Program
 {
     public static void Main(string[] args)
     {
         TcpListener server = new TcpListener(IPAddress.Parse("127.0.0.1"), 8080);
         server.Start();
         Console.WriteLine("Server is running 127.0.0.1:8080");
 
         TcpClient client = server.AcceptTcpClient();
         Console.WriteLine("Client accepted");
         NetworkStream stream = client.GetStream();
 
         //loop to handle the stream
         while (true)
         {
             while (!stream.DataAvailable) { };

             Byte[] bytes = new Byte[client.Available];
             stream.Read(bytes, 0, bytes.Length);
             Console.WriteLine("Data> " + Encoding.UTF8.GetString(bytes));
             var data = Encoding.UTF8.GetString(bytes);
 
              if (new Regex("^GET").IsMatch(data))
             {
                 Console.WriteLine("GET");
 
                 SHA1 sha1 = SHA1.Create();
                 Byte[] response = Encoding.UTF8.GetBytes(
                     "HTTP/1.1 101 Switching Protocols" + Environment.NewLine +
                     "Upgrade: websocket" + Environment.NewLine +
                     "Connection: Upgrade" + Environment.NewLine +
                     "Sec-WebSocket-Accept" + Convert.ToBase64String(
                         sha1.ComputeHash(
                             Encoding.UTF8.GetBytes(
                                 new Regex("Sec-WebSocket-Key: (.*)").Match(data).Groups[1].Value.Trim() +
                                 "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
                                 )
                         )
                     ) + Environment.NewLine + Environment.NewLine);
             }
             else
                 Console.WriteLine("!GET");
         }
     }
 }

And here is the client code:

 public class EchoTest : MonoBehaviour
 {    
     IEnumerator Start()
     {
         WebSocket w = new WebSocket(new Uri("ws://localhost:8080"));
         yield return StartCoroutine(w.Connect());
 
         w.SendString("Hi there");
         int i = 0;
         while (true)
         {
             string reply = w.RecvString();
             if (reply != null)
             {
                 Debug.Log("Received: " + reply);
                 t.text = reply;
                 w.SendString("Hi there" + i++);
             }
             if (w.error != null)
             {
                 Debug.LogError("Error: " + w.error);
                 w.Close();
 
                 yield return StartCoroutine(w.Connect());
                 break;
             }
             yield return 0;
         }
         w.Close();
     }
 }

When I run the server and unity, the server actually print the line "GET" on console, but nothing happens after. The unity then print the error message on block "if (w.error != null)":

Error: The WebSocket connection has already been closed. UnityEngine.Debug:LogError(Object) c__Iterator0:MoveNext() (at Assets/Example/EchoTest.cs:28)

How can I exchange messages between then? What am I doing wrong or missing?

Thanks.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image r3va · Feb 24, 2016 at 02:17 AM 0
Share

Hi. I've got the same problem. Did you find the solution?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SunnyChow · Feb 24, 2016 at 02:34 AM

I don't think Unity WebGL would support your plugin (which include a dll file), and some of the .NET classes you are using. I guess you probably should write a web socket feature in html way and make Unity to call it.

Also, did you test the server works? Anyways, I am using NodeJS to run my web socket server

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

WebGL sockets fails: Attempt to send to not connected connection 0 Answers

WebGL build with SocketIO throws errors 1 Answer

Message recieved Event Handler not available 1 Answer

Need help - Socket Server always connects to port 80 2 Answers

PUN Socket error 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges