0

只是在我的 Mac 上遇到问题,尝试使用此https://www.assetstore.unity3d.com/en/#!/content/38367通过 Web 套接字发送字符串

下面的许多改编代码主要来自这里http://www.codepool.biz/how-to-implement-a-java-websocket-server-for-image-transmission-with-jetty.html和 web socket sharp echotest 示例.

我可以连接,但在我的 Jetty 服务器控制台窗口(在运行 java(eclipse) 的 ws 服务器上)中没有字符串的迹象。

我现在基本上只是试图通过 websocket 连接与统一编辑器 (5) 向我的服务器发送一个“1”,以提示服务器开始发送编码为字节数组的 PNG 文件,这样我就可以把它们放回去一起在 C# 脚本中并将它们应用于纹理。

这是脚本,我想将它附加到像飞机或立方体这样的游戏对象上,并显示从我的 Jetty 服务器通过 Web 套接字发送的更新图像,但目前我只是试图发送消息和看到它在我的 Eclipse 控制台窗口中弹出。

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();
     }
 }

...以及来自码头服务器的相关代码,但这有效,我已经用一些 javascript 对其进行了测试,我可以将 PNG 加载回浏览器窗口,所以我在 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 );
         }
     }

任何帮助都将不胜感激,多年来一直潜伏在这里,希望能尽快上台,我也可以帮助其他人。本尼迪克特

编辑:这是我在 Unity 中的控制台错误消息

FormatException:长度无效。System.Convert.FromBase64String (System.String s) (在 /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Convert.cs:146) EchoTest+c__Iterator0.MoveNext ( ) (在资产/示例/EchoTest.cs:11)

我很确定问题来自于 webgl 的 websocket 尖锐。我需要将消息作为字节数组发送。

4

1 回答 1

0

好的,所以 Joakim Erdfelt 是对的,服务器没有配置为处理 Byte[] 消息。这是我为修复它而添加的内容:

@OnWebSocketMessage
public void onMessage(byte[] buffer, int offset, int length) throws UnsupportedEncodingException {
      System.out.println(buffer);
      String sFclientOutStr = new String(buffer, "UTF-8");
        sFclientOut = Integer.parseInt(sFclientOutStr);
      System.out.println(sFclientOut);
        if ((sFclientOut > 0) & (sFclientOut < 500)) {
            System.out.println("Part " + sFclientOut + " joined");  
            System.out.println( UIMain.usersPath + "/" + sFclientOutStr + ".png" );
            final String testVar = ( UIMain.usersPath + "/" + sFclientOutStr + ".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 (sFclientOutStr.equals("0")) {
            zerocounter = zerocounter + 1;
            if (zerocounter >= 2) {
                task.cancel();
            }
        }
        else if (sFclientOutStr.equals( "I'm client")) {
            System.out.println( "Client says: " + sFclientOutStr );
        }

}

这些链接帮助我解释了它http://www.programcreek.com/java-api-examples/index.php?api=org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage

http://www.eclipse.org/jetty/documentation/current/jetty-websocket-api-annotations.html

于 2015-12-10T00:38:36.213 回答