我使用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 預先感謝您
將崩潰logcat添加到您的問題。 –