erginduran
erginduran

Reputation: 1682

how to send data to nodejs server from android with socket.io module?

I wrote this node.js server.But when I send data from android with socket.io module.But I get errors.Please help me? how can ı send data(sender,message) from android to node.js server.

node.js server

var io = require('socket.io').listen(8001);
var connectCounter=0;
console.log('Server running :');

// open the socket connection
io.sockets.on('connection', function (socket) {
  connectCounter++;
  console.log('bağlantı başarılı sayı:');
  console.log(connectCounter);

  //Recode user online
  var sender;
  socket.on('user-online',function(data){

    console.log('user-online');

    var obj = JSON.parse(data);
    sender = obj.sender;

    console.log(sender);

    socket.broadcast.emit('online',{sender : sender});
  });

    socket.on('message', function(message){

    console.log('on-message');

    var obj = JSON.parse(message);
        socket.broadcast.emit('event',{sender : obj.sender, message : obj.message});
    console.log(message);
    });

  socket.on('disconnect', function (socket){

     console.log('disconnect');
     connectCounter--;
    socket.broadcast.emit('offline',{sender : sender})
  });

});

It's a android codes.

btnOK.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            strUserName = edtUserName.getText().toString();
            if (strUserName != null && strUserName.length() > 0 || !"".equals(strUserName)) {
                Intent intent = new Intent(getApplicationContext(), ScreenChat.class);
                intent.putExtra("UserName", strUserName);
                startActivity(intent);
                edtUserName.getText().clear();
            }else {
                Toast.makeText(getApplicationContext(), "user name null", Toast.LENGTH_SHORT).show();
                return;
            }

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

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

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

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

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

                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 e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }

        @SuppressWarnings("unused")
        public void onClick(final DialogInterface arg0, final int arg1) {
            // TODO Auto-generated method stub

        }
    });

When I click btnOK(you can see above code), I get this error: error screen on windows 7 console prompt

Upvotes: 1

Views: 3693

Answers (1)

ksloan
ksloan

Reputation: 1492

You are sending a string, and trying to parse it as JSON.

socket.send("Hello Server!");

var obj = JSON.parse(message);

"Hello Server!" is not valid JSON.

Upvotes: 2

Related Questions