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 /
  • Help Room /
avatar image
1
Question by bitdigger · Nov 08, 2015 at 01:57 PM · c#networkingwebgltexture2djava

Using websockets through the simple websockets for webgl asset, can connect but cant transmit messages.

Hey all,

Just having a problem on my mac trying to send strings over web sockets using this https://www.assetstore.unity3d.com/en/#!/content/38367

Lots of adapted code below from here mainly http://www.codepool.biz/how-to-implement-a-java-websocket-server-for-image-transmission-with-jetty.html and the web socket sharp echotest example.

I can connect but there is no sign of strings in my Jetty server console window (on a ws server running in java(eclipse)).

I’m basically just trying to send a ā€œ1ā€ to my server over a websocket connection with the unity editor (5) at the moment, to prompt the server to start sending PNG files encoded as byte arrays, so I can put them back together in a C# script and apply them to a texture.

this is the script, I want to attach it to a game object like a plane or a cube and display the updating images sent over the web socket from my Jetty server, but at the moment I'm just stuck trying to send a message and see it pop up in my eclipse console window.

 using UnityEngine;
 using System.Collections;
 using System;
 
 public class socketTexture : MonoBehaviour {
 
     // Use this for initialization
     IEnumerator Start () {
         WebSocket w = new WebSocket(new Uri("ws://192.168.0.149:8080/"));
         yield return StartCoroutine(w.Connect());
         Debug.Log ("Connected");
         w.SendString("I'm client");
         w.SendString("1");
         while (true)
         {
             byte[] reply = w.Recv();
             if (reply != null)
             {
                 Debug.Log ("Received: "+reply);
                 var tex = new Texture2D(300, 300, TextureFormat.PVRTC_RGBA4, false);
                 // Load data into the texture and upload it to the GPU.
                 tex.LoadRawTextureData(reply);
                 tex.Apply();
                 // Assign texture to renderer's material.
                 GetComponent<Renderer>().material.mainTexture = tex;
             }
             if (w.Error != null)
             {
                 Debug.LogError ("Error: "+w.Error);
                 break;
             }
             yield return 0;
         }
         w.Close();
     }
 }

...And the relevant code from the jetty server, but this works, I've tested it with some javascript and I can load the PNGs back into the browser window, so I'm definitely doing something wrong in Unity

 @OnWebSocketMessage  //part request from websocket client (remote browser)
     public void onMessage( String message) {
         System.out.println("message");
         if (message.equals("1") || message.equals("2") || message.equals("3") || message.equals("4") ) {
             System.out.println("Part " + message + " joined");  
             System.out.println( UIMain.usersPath + "/" + message + ".png" );
             final String testVar = ( UIMain.usersPath + "/" + message + ".png" );
             task = new FileWatcher( new File(testVar) ) {
                 protected void onChange( File file ) {
                     // here we code the action on a change
                     System.out.println( "File "+ file.getName() +" has changed!" );
                     try {            
                         File f = new File(testVar);
                         BufferedImage bi = ImageIO.read(f);
                         ByteArrayOutputStream out = new ByteArrayOutputStream();
                         ImageIO.write(bi, "png", out);
                         ByteBuffer byteBuffer = ByteBuffer.wrap(out.toByteArray());
                         mSession.getRemote().sendBytes(byteBuffer);
                         out.close();
                         byteBuffer.clear();
                         }
                            catch (IOException e) {
                                e.printStackTrace();
                         }    
             }
         };
         Timer timer1 = new Timer(); {
         timer1.schedule(task , new Date(), 40 );
         }
         
         }
         else if (message.equals( "0")) {
             zerocounter = zerocounter + 1;
             if (zerocounter >= 2) {
                 task.cancel();
             }
         }
         else if (message.equals( "Hi there, client here")) {
             System.out.println( "Client says: " + message );
         }
     }

Cheers in advance. Benedict

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
Best Answer

Answer by saltyJeff · Dec 26, 2015 at 02:58 AM

Unity3d sends it as a bytestream, if you were using the same nodeJS websocket script as I am https://www.npmjs.com/package/nodejs-websocket use the 'binary' event instead of the 'text' event Use a special character to divide your messages and look for it to define a message, then convert the bytes to characters to get the string sent.

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
avatar image
0

Answer by bitdigger · Nov 09, 2015 at 09:46 AM

Ok doesn't seem like anyone is interested in this so far, but my own progress has been to get the PNG file byte code transmitting to Unity via the websocket connection and showing up as a texture! last trick is now to get Unity sending a string back to my server and unfortunately it seems like I can't work out the formatting used by websocket sharp. using websocket.send in javascript it works. when I use websocket.SendString() in websocket sharp the server doesn't see the message. stuck on this one for a few days any help would be appreciated :)))

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

43 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

Related Questions

How do I enable CORS in my webgl game? 0 Answers

Day/Night cycle turns black at night and how to Serialize it rightly? 0 Answers

Photon Networking - How to move all players from game scene to current room scene? 0 Answers

Calling Bool in Network Command Not Working 0 Answers

How to choose different teams one by one 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