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
1
Question by nug700 · Nov 14, 2015 at 12:10 AM · c#networkingwebgl

jslib from WebSocket doesn't receive data

I'm trying to use the websocket package unity provides from the asset store to connect to my server from WebGL, but for some reason I cannot receive messages; only send them when testing in the browers , but works fine in the editor. I've found the reason it works in the editor is because in all other platforms except WebGL, it uses websocket-sharp.dll, and in WebGL it replaces the dll for a jslib.

The client WebSocket wrapper

The client echo script

The server code

Any help appreciated, thanks!

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by EarthLaunch · Jan 03, 2016 at 09:26 AM

Latest Nov 2015 version is missing string receive handling (and a warning about uncaught types...), to hack fix it add this in WebSocket.jslib around line 30, tacked onto the if/else for onmessage handling:

         else if(typeof e.data === "string") {
             var reader = new FileReader();
             reader.addEventListener("loadend", function() {
                 var array = new Uint8Array(reader.result);
                 socket.messages.push(array);
             });
             var blob = new Blob([e.data]);
             reader.readAsArrayBuffer(blob);
         }

Credit goes to https://github.com/sta/websocket-sharp/issues/181#issuecomment-158873401

Comment
Add comment · Show 1 · 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
avatar image zhuk1011 · Oct 24, 2016 at 11:49 AM 1
Share

Thanks!!!!

avatar image
1

Answer by WilliamLeu · Feb 09, 2018 at 08:22 PM

EarthLaunch's answer works and it helped me out, but I had a problem that took me a while to figure out, FileReader is asynchronous - and there's really no synchronous/blocking way to do the same code.

Why does that matter? Because large messages that were sent earlier took longer to process, and messages were being processed out of order. This matters if you're sending messages to create objects, and messages to modify and process those objects, and then you're trying to modify objects before the webapp even created them and your app breaks (even though it works fine in Unity... best type of bug...).

So to anyone who needs assurance that the messages are processed in order, I added another modification. If there's a more elegant way to make sure messages are processed in order, I'd love to know.

At the top, two new variables need to be added, an array for the order of buffered reads, and a set for those that are finished.

 SocketCreate: function(url)
 {
     var str = Pointer_stringify(url);
     var socket = {
         socket: new WebSocket(str),
         buffer: new Uint8Array(0),
         error: null,
         messages: [],
         readerqueue: [],
         finishedreads : new Set()
     }

And every time we finish reading, we log ourselves as finished and flush the items in order that are finished.

 else if(typeof e.data === "string") 
 {    
      var reader = new FileReader();
      reader.addEventListener(
         "loadend", 
         function() 
          {
                 socket.finishedreads.add(reader);
 
                 // Make sure messages are added in order.
                 // For reasons of javascript etiquete, we couldn't
                 // have 
                 while(socket.readerqueue.length != 0)
                 {
                     var topItem = socket.readerqueue[0];
 
                     if( socket.finishedreads.has(topItem))
                     {
                         var array = new Uint8Array(topItem.result);
                         socket.messages.push(array);
 
                         socket.readerqueue.shift();
                         socket.finishedreads.delete(topItem);
                     }
                     else
                         break;
          }
      });
 
      var blob = new Blob([e.data]);
      socket.readerqueue.push(reader)
      reader.readAsArrayBuffer(blob);
  }

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

44 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Browser interaction to Unity 0 Answers

What headers can be passed through in web gl 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Unity3d Csharp Networking Error 3 Answers


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