java - Kryonet connection successful but not receiving messages -
hi using kryonet network library game i'm developing.
i've got code in server side:
public class myserver { kryo kryo; server server; connection connection; public myserver(){ server = new server(); server.start(); try { server.bind(59990, 59900); } catch (ioexception e) { system.out.println("fatal error"); } kryo = server.getkryo(); kryo.register(message.class); server.addlistener(new listener() { public void received (connection con, message str) { system.out.println("message recieved client server"); message m = (message)str; system.out.println(m.text); } public void connected(connection con){ system.out.println("hey, connected!"); connection = con; con.sendtcp("hola cliente!!"); } public void disconnected(connection con){ system.out.println(":( gone... he'll never come back.."); } }); } public void sendmessage(string tipomensaje,string tipotropa,string id){ connection.sendtcp(tipomensaje+"-"+tipotropa+"-"+id); } }
and in client side:
public class myclient { client client; connection connection; kryo kryo; public myclient(){ client = new client(); client.start(); try { kryo = client.getkryo(); kryo.register(message.class); client.connect(5000, "10.211.55.3", 59990, 59900); //sendmessage("fromclient","ee","ee"); client.addlistener(new listener() { public void received (connection con, string str) { system.out.println(str); system.out.println("message recieved server client"); } public void connected(connection con){ system.out.println("hey, connected!"); connection = con; message m = new message(); m.text="eeep"; client.sendtcp(m); } public void disconnected(connection con){ system.out.println(":( gone... he'll never come back.."); } }); } catch (ioexception e) { system.out.println("error while connecting"); } } public void sendmessage(string tipomensaje,string tipotropa,string id){ client.sendtcp(tipomensaje+"-"+tipotropa+"-"+id); system.out.println("message sent client server"); } }
message class string in it. when try connect client server "hey, connected!" message , afterwards "message recieved client server" in server's command line. however, m.text (the content of message) not printed , don't know why.
thanks in advance.
it's because there no method public void received (connection con, string str)
in listener - basically, server never calls it. reason why should use @override
- wouldn't have problem in first place.
replace string str
object o
. - check if object message
. if is, cast message
, print out text. method should this:
@override public void received(connection connection, object packet) { if (packet instanceof message) { system.out.println(((message) packet).text); } }
Comments
Post a Comment