我的mac尝试使用此https://www.assetstore.unity3d.com/en/#!/content/38367
在网络套接字上发送字符串时出现问题下面有很多改编的代码,主要是http://www.codepool.biz/how-to-implement-a-java-websocket-server-for-image-transmission-with-jetty.html和网络插座尖锐的回声示例。
我可以连接,但我的Jetty服务器控制台窗口中没有字符串的迹象(在java(eclipse)中运行的ws服务器上)。
我基本上只是尝试通过与Unity编辑器(5)的websocket连接向我的服务器发送“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();
}
}
...来自jetty服务器的相关代码,但是这个工作,我用一些javascript测试了它,我可以将PNG加载回浏览器窗口,所以我肯定在Unity中做错了什么< / p>
@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)(at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Convert.cs:146) EchoTest + c__Iterator0.MoveNext()(at 资产/实施例/ EchoTest.cs:11)
我很确定问题来自webgl的websocket sharp。我需要将消息作为字节数组发送。
答案 0 :(得分: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.eclipse.org/jetty/documentation/current/jetty-websocket-api-annotations.html