有没有一种方法可以让一个简单的way服务器发送消息给团结?
目前,我们正在更新方法中使用GET使用UnityWebRequest.Get()。下面是代码:
// Update is called once per frame
void Update () {
StartCoroutine(GetData());
}
IEnumerator GetData()
{
UnityWebRequest uwr = UnityWebRequest.Get(url);
yield return uwr.Send();
if (uwr.isError)
{
Debug.Log(uwr.error);
}else
{
Debug.Log((float.Parse(uwr.downloadHandler.text) / 100));
fnumber = ((float.Parse(uwr.downloadHandler.text) / 100));
transform.position.Set(oldX, fnumber, oldZ);
}
}但是,这会引发此错误:
无法解析目标主机
我发现了这 bug-report,它声明,它应该已经修复了,但看起来并非如此。
那么,是否有一种方式让服务器发送消息给团结呢?
谢谢
发布于 2017-03-31 10:31:03
为了使我的评论更具描述性和具体性,我的建议是创建一个简单的Thread,作为与您的服务器的通信管理器。
首先,您必须实现这个类来调用Unity线程上的方法。
当您实现它时,只需创建一个类:
public class CommunicationManager
{
static readonly object syncRoot = new object();
static CommunicationManager _Instance;
public static CommunicationManager Instance
{
get
{
if ( _Instance == null )
{
lock ( syncRoot )
{
if ( _Instance == null )
{
_Instance = new CommunicationManager();
}
}
}
return _Instance;
}
}
volatile bool working = false;
Queue<Message> _messages;
private CommunicationManager()
{
_messages = new Queue<Message>();
InitializeCommunication();
}
void InitializeCommunication()
{
Thread t = new Thread(CommunicationLoop);
t.Start();
}
void CommunicationLoop()
{
Socket s = null;
try
{
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1337));
working = true;
}
catch(Exception ex)
{
working = false;
}
while ( working )
{
lock ( syncRoot )
{
while ( _messages.Count > 0 )
{
Message message = _messages.Dequeue();
MessageResult result = message.Process(s);
result.Notify();
}
}
Thread.Sleep(100);
}
}
public void EnqueueMessage(Message message)
{
lock ( syncRoot )
{
_messages.Enqueue( message );
}
}
}现在,您必须使Message和MessageResult对象:
public class Message
{
const string REQUEST_SCHEME = "{0} {1} HTTP/1.1\r\nHost: {hostname}\r\nContent-Length: 0\r\n\r\n";
string request;
Action<MessageResult> whenCompleted;
public Message(string requestUri, string requestType, Action<MessageResult> whenCompleted)
{
request = string.Format(REQUEST_SCHEME, requestType, requestUri);
this.whenCompleted = whenCompleted;
}
public MessageResult Process(Socket s)
{
IPEndPoint endPoint = (IPEndPoint)s.RemoteEndPoint;
IPAddress ipAddress = endPoint.Address;
request = request.Replace("{hostname}", ipAddress.ToString());
s.Send(Encoding.UTF8.GetBytes(request));
// receive header here which should look somewhat like this :
/*
HTTP/1.1 <status>
Date: <date>
<some additional info>
Accept-Ranges: bytes
Content-Length: <CONTENT_LENGTH>
<some additional info>
Content-Type: <content mime type>
*/
// when you receive this all that matters is <CONTENT_LENGTH>
int contentLength = <CONTENT_LENGTH>;
byte[] msgBuffer = new byte[contentLength];
if (s.Receive(msgBuffer) != contentLength )
{
return null;
}
return new MessageResult(msgBuffer, whenCompleted);
}
}最后创建MessageResult对象:
public class MessageResult
{
Action<MessageResult> notifier;
byte[] messageBuffer;
public MessageResult(byte[] message, Action<MessageResult> notifier)
{
notifier = notifier;
messageBuffer = message;
}
public void Notify()
{
UnityThread.executeInUpdate(() =>
{
notifier(this);
});
}
}此方法将从您的主要应用程序运行,这样就不会产生任何冻结和任何东西。
要使用它,您只需调用这样的东西:
Message message = new Message("/index.html", "GET", IndexDownloaded);
CommunicationManager.Instance.EnqueueMessage(message);
// ...
public void IndexDownloaded(MessageResult result)
{
// result available here
}有关更多信息,请阅读这个维基百科关于HTTP的文章。
发布于 2017-03-31 10:10:34
这是从RESTful服务器获取请求的简单方法:D。
private string m_URL = "https://jsonblob.com/api/d58d4507-15f7-11e7-a0ba-014f05ea0ed4";
IEnumerator Start () {
var webRequest = new WWW (m_URL);
yield return webRequest;
if (webRequest.error != null) {
Debug.Log (webRequest.error);
} else {
Debug.Log (webRequest.text);
}
}发布于 2017-03-31 10:06:54
尝试使用WWW调用,而不是UnityWebRequest,这是坏的。你用的是什么版本的统一?
https://stackoverflow.com/questions/43137183
复制相似问题