Armory3D中如何实现network通讯

对于Armory3D中如何实现网络通讯,尝试过以下几个方法,目前只有一种方法是可行的。

  • 使用haxe.http包
package;

class Main {
	static function main() {
		trace("Hello, world!");

		var http = new haxe.Http("http://localhost:8080/test");

		http.onData = function(data:String) {
			trace(data);
		}

		http.onError = function(error) {
			trace('error: $error');
		}

		http.request();
	}
}

调试时会报错, 原因未解:

Trace: Error: Unable to create XMLHttpRequest object.
    at Function.js_Browser.createXMLHttpRequest (<anonymous>:19945:8)
    at haxe_http_HttpJs.request (<anonymous>:1657:33)
    at <anonymous>:648:8
    at iron_App.render (<anonymous>:2425:5)
    at Function.kha_System.render (<anonymous>:21870:3)
    at kha_SystemImpl.renderCallback (<anonymous>:22012:13)
  • 使用kha.netsync库
package arm;

import kha.netsync.Session;
import kha.netsync.Network;

class TestTrait extends iron.Trait {
	static var s = new Session(1000, "192.168.2.244", 8080);

	public function new() {
		super();
		trace(s.network);
		s.network = new Network("192.168.2.244", 8080, null, null); // this.onNetworkError, this.onNetworkClose);

		notifyOnInit(function() {
		});

		// notifyOnUpdate(function() {
		// });

		// notifyOnRemove(function() {
		// });
	}

	public function onNetworkError() {}

	public function onNetworkClose() {}
}

调试时会报错, 原因未解:

Trace: TypeError: Cannot read property 'Network' of undefined
    at new arm_TestTrait (<anonymous>:640:26)
    at Function.Type.createInstance (<anonymous>:228:9)
    at Function.iron_Scene.createTraitClassInstance (<anonymous>:3683:14)
    at Function.iron_Scene.createTraits (<anonymous>:3610:31)
    at iron_Scene.returnObjectLoaded (<anonymous>:4330:15)
    at iron_Scene.returnObject (<anonymous>:4294:9)
    at <anonymous>:4265:11
    at <anonymous>:4679:4
    at <anonymous>:6115:4
    at new iron_data_MeshData (<anonymous>:6094:2)

  • 使用haxe的sys.net.Socket
package arm;

class NetworkTrait extends iron.Trait {
	static var mClient = new sys.net.Socket();

	public function new() {
		super();

		trace("To connect to tcp server...");
		mClient.connect(new sys.net.Host("localhost"), 3001);
		mClient.write("Hello, I'm Armory!");

		#if (target.threaded)
		sys.thread.Thread.create(() -> {
			while(true) {
				mClient.write("I'm waiting your talk!");
				mClient.waitForRead();
				var msg = mClient.input.readLine();
				trace(msg);
			}
		});
		#end
	}
}

调试时程序可正常进行网络通讯,参与调试的服务端测试代码如下:

const net = require('net');

var server = net.createServer();
server.on('connection', function(socket){
   console.log('client connected to server');
   socket.on('data', function(data){
       console.log('received data from client is:' + data);
	   
	   setTimeout(()=>{
		   console.log("to send data");
		   // 按行发送数据,以\n分割
		   socket.write('confirmed data ' + data + '\n');
	   }, 1000);
   });
});
server.listen(3001, 'localhost');
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: Age of Ai 设计师: meimeiellie
应支付0元
点击重新获取
扫码支付

支付成功即可阅读