Friday 14 April 2017

Retrieve socket emit in android Studio

I create a socket with nodejs ,

this socket emit a message "connection ok ! " this message work in my console android but i can't display this message on a TextView

he display nothing someone can help me to display this message on a TextView??

Mycode

 public class MainActivity extends AppCompatActivity {
  TextView MessageSocket;
  @Override
     protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MessageSocket= (TextView) findViewById(R.id.MessageSocket);
    runIO();
}


public void runIO(){
    try {
        SocketIO socket = new SocketIO("http://10.10.103.36:8080");
        socket.connect(new IOCallback() {
            @Override
            public void onMessage(JSONObject json, IOAcknowledge ack) {
                try {
                    System.out.println("Server said:" + json.toString(2));
                    MessageSocket.setText(json.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onMessage(String data, IOAcknowledge ack) {
                System.out.println("Server said: " + data);

            }

            @Override
            public void onError(SocketIOException socketIOException) {
                System.out.println("an Error occured");
                socketIOException.printStackTrace();
            }

            @Override
            public void onDisconnect() {
               System.out.println("Connection terminated.");
            }

            @Override
            public void onConnect() {
                System.out.println("Connection established");
            }

            @Override
            public void on(String event, IOAcknowledge ack, Object... args) {
                System.out.println("Server triggered event '" + event + "'");
            }
        });

        // This line is cached until the connection is establisched.
        socket.send("Hello Server!");
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

my files javascript

var http = require('http');

var fs = require('fs');




 var server = http.createServer(function(req, res) {

 fs.readFile('./index.html', 'utf-8', function(error, content) {

    res.writeHead(200, {"Content-Type": "text/html"});

    res.end(content);

  });

  });



  var io = require('socket.io').listen(server);



io.sockets.on('connection', function (socket) {

  console.log('Connection ok!');
   io.sockets.on('connection', function (socket) {
    socket.emit('message', 'Vous ĂȘtes bien connectĂ© !');
  });

  });



  server.listen(8080);



via Axel Cherifi

No comments:

Post a Comment