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 Unity Asset Store - The Best Assets for Game Making

Lots of adapted code below from here mainly How to Implement a Java WebSocket Server for Image Transmission with Jetty | Dynamsoft Developers Blog 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://"));
        yield return StartCoroutine(w.Connect());
        Debug.Log ("Connected");
        w.SendString("I'm client");
        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.
                // Assign texture to renderer's material.
                GetComponent<Renderer>().material.mainTexture = tex;
            if (w.Error != null)
                Debug.LogError ("Error: "+w.Error);
            yield return 0;

…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) {
    	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 =;
            			ByteArrayOutputStream out = new ByteArrayOutputStream();
            			ImageIO.write(bi, "png", out);
    					ByteBuffer byteBuffer = ByteBuffer.wrap(out.toByteArray());
    			       	catch (IOException e) {
    	Timer timer1 = new Timer(); {
    	timer1.schedule(task , new Date(), 40 );
    	else if (message.equals( "0")) {
    		zerocounter = zerocounter + 1;
    		if (zerocounter >= 2) {
    	else if (message.equals( "Hi there, client here")) {
    		System.out.println( "Client says: " + message );

Cheers in advance.

Unity3d sends it as a bytestream, if you were using the same nodeJS websocket script as I am

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.

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 :)))