2016-11-28 87 views
0

我使用socket.io(Node.js的)制作的机器人聊天应用程序和客户端将在Javasocket.io服务器编码和客户端与Android应用

,我已经进口https://github.com/nkzawa/socket.io-android-chat 仓库从GitHub

客户端

public class MainActivity extends AppCompatActivity { 
    SocketIO socket; 
    Button b,b1,b3; 
    EditText e; 
    TextView t; 
    Socket sock; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    b=(Button)findViewById(R.id.button); 
    b1=(Button)findViewById(R.id.button2); 
    b3=(Button)findViewById(R.id.button3); 
    e=(EditText)findViewById(R.id.editText); 
    b.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { // this is the button when pressed it will connect to server 
      socket = new SocketIO(); 
     } 
    }); 

    b1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String s=e.getText().toString(); 
      System.out.println(s); 
      socket.sendMessage(s);   // it is the button when pressed it sends msg 
     } 
    }); 
} 
public class SocketIO{ 
    public SocketIO(){ 
     try{ 
      sock = IO.socket("http://31.220.111.101:3000/"); 
      sock.connect(); // connect to server 
      Toast.makeText(getApplicationContext(),"Connected",Toast.LENGTH_LONG).show(); 
      sock.on("new message",msg);  // it is being made to recieve msg 
     } catch(Exception e){ 
      Toast.makeText(getApplicationContext(),"Got a exception",Toast.LENGTH_LONG).show(); 
     } 
    } 
    public void sendMessage(String s) 
    { 
     Toast.makeText(getApplicationContext(),"Sent ",Toast.LENGTH_LONG).show(); 
     sock.emit("send message",s); 
    } 

} 
private Emitter.Listener msg=new Emitter.Listener() { 
    @Override 
    public void call(Object... args) { 
     t.setText("Some message received "); 
    } 
}; 

}

而且服务器代码即Node.js的代码是

var express=require('express'); 
var app=express(); 
var server=require('http').createServer(app); 
var io= require('socket.io').listen(server); 
users=[]; 
connections=[]; 

server.listen(process.env.PORT || 3000); 
console.log('chal hua'); 

app.get('/',function(req,res){ 
res.sendFile(__dirname+'/index.html'); 
}); 

io.sockets.on('connection',function(socket){ 
    connections.push(socket); 
    console.log('connected %s',socket.id); 

    socket.on('disconnect',function(){ 
    connections.splice(connections.indexOf(socket),1); 
    console.log('Disconnected'); 
    }); 

    socket.on('send message',function(data){ 
    console.log(data); 
    io.emit('new message',{msg:data}); 
}); 

}); 

我现在面临的问题是,我能够连接到我的服务器也是我能够将消息发送到服务器,但无法从服务器接收邮件时,它必须接收消息的Android应用程序破坏自动

我还上传了Android的代码,GitHub上,如果你想看看那么你可以

https://github.com/harshitsharma0003/Helloprt 预先感谢您

+0

将崩溃logcat添加到您的问题。 –

回答

0

试试这个。

var express=require('express'); 
var app=express(); 
var server=require('http').Server(app); 
var io= require('socket.io')(server); 
users=[]; 
connections=[]; 

server.listen(process.env.PORT || 3000); 
console.log('chal hua'); 

app.get('/',function(req,res){ 
res.sendFile(__dirname+'/index.html'); 
}); 

io.on('connection',function(socket){ 
    connections.push(socket); 
    console.log('connected %s',socket.id); 

    socket.on('disconnect',function(){ 
    connections.splice(connections.indexOf(socket),1); 
    console.log('Disconnected'); 
    }); 

    socket.on('send message',function(data){ 
    console.log(data); 
    io.emit('new message',{msg:data}); 
}); 

}); 
+1

OP为什么要尝试你的解决方案?你能否提供一个解释,以便于理解。 –

+0

先生让我告诉你,当我通过相同的服务器代码制作一个网络应用程序,它运作良好,但有一个问题在Android客户端 –