Socket.io android java client receiving messages and sending file example -
Socket.io android java client receiving messages and sending file example -
does have sample code demonstrates receiving messages on java client side socket.io?
also, there illustration on sending file/binary/picture on same socket.io java client? (basically sample code java instead of javascript client)
the version of android java client can acquired here (this version claim can used socket.io 1.0 , later) (seems updated version) https://github.com/nkzawa/socket.io-client.java
currently sample code allows me initialize connection, server able incoming connection event , java socket.io client able send out basic emit message. however, there no plain simple examples on how acquire message update server broadcast or emits website user.
sample code reference:
package com.sample.d10132014a; import java.io.bufferedreader; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.inputstream; import java.io.inputstreamreader; import java.net.urisyntaxexception; import org.json.jsonexception; import org.json.jsonobject; import android.app.activity; import android.os.bundle; import android.util.log; import android.view.menu; import android.view.menuitem; import com.github.nkzawa.socketio.client.*; // java socket io client import com.github.nkzawa.socketio.client.socket; import com.github.nkzawa.emitter.emitter; import com.github.nkzawa.engineio.client.*; // java engine io client import com.github.nkzawa.engineio.client.transports.*; public class mainactivity extends activity { public static string internalpath; // internal storage path public static string filename; // file name private socket socket; // socket object @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); seek { socket = io.socket("http://yourserverip:3000"); socket.connect(); // initiate connection socket server socket.emit("chat message", "from android server: 1st outgoing message"); } grab (urisyntaxexception e) { e.printstacktrace(); } socket.on(socket.event_connect, new emitter.listener() { @override public void call(object... args) { log.d("socketio", "socket connected"); socket.emit("chat message", "even connect: message sent android socketio server"); //socket.disconnect(); // why there disconnect here? } }).on("chat message", new emitter.listener() { @override public void call(object... arg0) { // todo auto-generated method stub jsonobject obj = (jsonobject)arg0[0]; log.d("socketio", "message back: "+obj.tostring()); log.d("socketio", "incomming chat message: " + obj.tostring() + arg0 + arg0[0] + arg0[1]); // trying test variable holds message } }).on(socket.event_message, new emitter.listener() { @override public void call(object... arg0) { // todo auto-generated method stub log.d("socketio", "socket event message" + arg0); socket.emit("chat message", "android server event message"); } }); // 2nd segment test without connecting 1 long method socket.on(socket.event_connect_error, new emitter.listener() { @override public void call(object... arg0) { // todo auto-generated method stub log.d("socketio", "socket event connect error"); socket.emit("chat message", "android server: socket event connect error"); } }); socket.on(socket.event_message, new emitter.listener() { @override public void call(object... arg0) { // todo auto-generated method stub log.d("socketio", "socket event message" + arg0); socket.emit("chat message", "android server event message"); } }); setcontentview(r.layout.activity_main); } // ending oncreate method } // ending class
thanks reading
hy not sure if u looking for, , there might chance have solved it, regardless reply when browsing solution problem, came across question here couldnot find answer, left me disappointed. have solved problem, share how did it.
my problem was receiving message node.js server see message in logcat , having problem print message on android application in main ui thread.
lets say, show message received server in list view.
try { socket = io.socket("http://192.168.1.10:3000"); } grab (urisyntaxexception e) { e.printstacktrace(); } socket.on(socket.event_connect, new emitter.listener() { @override public void call(object... args) { log.d("activityname: ", "socket connected"); // emit want here server socket.emit("login", some); //socket.disconnect(); } // emit server }).on("somefunction", new emitter.listener() { @override public void call(object... args) { // argas[0] can have type send server jsonarray obj = (jsonarray) args[0]; string message = obj.tostring(); // runonuithread needed if want alter in ui thread runonuithread(new runnable() { public void run() { // //mlistdata array adapter mlistdata.add("serversays" + " : " + " " + message); mlistdata.notifydatasetchanged(); lvlist.setselection(mlistdata.getcount() -1); } }); } }).on(socket.event_disconnect, new emitter.listener() { @override public void call(object... args) { log.d("activityname: ", "socket disconnected"); } }); socket.connect();
we can update views main thread. have move portion of background task updates ui onto main thread.so have add together next , our required task in there.
runonuithread(new runnable(){ @override public void run() { //do } }
hope save time someone.
java android socket.io
Comments
Post a Comment