Questions About Networking


#1

I recently (or, if you consider when I started, not all that recently) made a tcp server for a simple multiplayer game, as many people do. While I’m able to interact with this server normally and in an expected manner, I’m still stuck on connecting to it via Armory. I did some digging, and I found that the kha package has (some) networking capability. I made the following trait (complete with intellectually stimulating print statements for debug):

//Manage.hx
package arm;
import haxe.Json;
import iron.Scene;
import haxe.io.Bytes;
import haxe.Timer;
import haxe.io.BytesData;
import kha.network.Session;
import kha.network.Network;
class Manage extends iron.Trait {
	static var netBuff:Int=8192;

	static var user:String ="haxer";
	static var passwd:String ="I_have_a_life.";
	static var s=new Session(1024,"192.168.2.165",1251);
	static var commands:String;
	static var readme:Dynamic;
	static var first:Bool=true;
	public function splitbychar(split:String,by:String):Array<String> {
		var end:Array<String>=new Array<String>();
		var endi:Int=0;
		for (element in 0...split.length) {
			if (split.charAt(element)==by) {
				endi++;
			} else {
				end[endi]+=split.charAt(element);
			}
		}
		return end;

	}
	public function new() {
		super();
		trace("Hello world");
		s.network=new Network("192.168.2.165",1251,null,null);

		notifyOnInit(function() {
			
		});

		notifyOnUpdate(function() {
			s.update();
			var buff:Bytes=Bytes.alloc(netBuff);
			buff.fill(0,netBuff,0);
			trace(buff);
			trace("hi");
			if (first) {
				s.waitForStart(function(){trace(1);},function(){trace(2);},function(){trace(3);},function(){trace(4);},function(){trace(5);});
				var tosend:Bytes=Bytes.ofString(passwd+" "+user+"@");
				trace(tosend);
				s.sendToServer(tosend);
				trace("meh");
				s.receive(buff,null);
				trace("feh");
				var ack=splitbychar(buff.toString(),"@")[0];
				if (ack!="initack"){//I don't know how to exit, so this will have to do.
					var crash=[0];
					crash[1]=0;
				}
				first=false;
			} else {
				trace("cool");
				//send stuff to server
				s.sendToServer(Bytes.ofString(commands+"@"));
				trace("double feh");
				//read and deploy to trait so other haxe scripts can read from here
				var incoming:String;
				var incomingbytes:Bytes=Bytes.alloc(netBuff);
				incomingbytes.fill(0,netBuff,0);
				s.receive(buff,null);
				incoming=buff.toString();
				trace(incoming);
				var args=splitbychar(incoming," ");
				if (args.length>0) {
					if (args[0]=="json") {
						readme=Json.parse(args[1]);
						for (element in Reflect.fields(readme.data)){
							var ob:Dynamic=Reflect.field(readme.data,element);
							var ignore:Bool=false;
							for (objectgroup in Scene.active.groups) {
								for (object in objectgroup) {
									if (object.name==ob.Name){
										ignore=true;
										break;
									}
								}
							}
							if (!ignore) {
								var obj:iron.object.Object=null;
								obj.name=ob.Name;
								Scene.active.addObject(obj);
							}
						}
					}
				}
				//then input (fills commands)
			}
		});

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

The output keeps giving ones, implying that there is a callback on waitForStart, and the server appears to be sending back nothing (surprise, surprise). I have a sneaking suspicion I’m going about this completely wrong, but I haven’t been at this long, so I was wondering if more experienced and competent people could point me in the right direction to simple tcp text exchange in Haxe.
If you haven’t guessed, I’m pretty new to Armory and Haxe.
Thanks in advance.


"Libraries" folder does nothing? [Solved]
#2

As a side note, I did download the tank networking tutorial, but it made little sense, it seemed to depend on a server that was also familiar with armory internals.


#3

Also, before we question the server, the client works in the bge while sending the sever input:


Sorry about all of these posts, had a lot of afterthoughts.


#4

Yet another post: I have discovered the tink-tcp package, but run into this error:

 Error: Library tink-tcp not found.
 Add it to the 'Libraries' subdirectory of your project. You may also install it via haxelib but that's less cool.

I’ll be doing some more research on this, even in “Libraries” it does nothing.


#5

nvm, this was probably due to my incorrect source path. At any rate, I’m doing this with wasm now.