I had a question, I built the server part and the client with a socket and did not receive any errors. When I type the local address in the browser, it shows the information in cmd, but when I run the Android application in the emulator or mobile, it does not connect to the server.
The permissions I used:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Server codes:
const app = require('express')();
const http = require('http').Server(app);
const io = require('socket.io')(http);
//app.use(express.static(__dirname + '/static')
app.get('/', function (req, res, next) {
res.sendFile(__dirname + '/static/index.html');
});
io.on('connection', function (socket) {
console.log('one user connected ' + socket.id);
socket.on('message',function (data) {
console.log(data);
var sockets=io.sockets.sockets;
sockets.forEach(function (item) {
item.emit('message',{message:data});
});
});
socket.on('disconnect', function (){
console.log('user disconnected');
})
});
http.listen(8000)
console.log('server run on port 8000');
The MainActivity.java is:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.github.nkzawa.emitter.Emitter;
import com.github.nkzawa.socketio.client.IO;
import com.github.nkzawa.socketio.client.Socket;
import org.json.JSONException;
import org.json.JSONObject;
import java.net.URISyntaxException;
public class MainActivity extends AppCompatActivity {
private Socket socket;
{
try {
socket = IO.socket("http://192.168.42.51:8000");
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
Button btnSend;
EditText edtTextMessage;
LinearLayout linearMessage;
LinearLayout.LayoutParams layoutParams;
public Handler handler;
@Override
protected void onDestroy() {
super.onDestroy();
socket.disconnect();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSend = (Button) findViewById(R.id.btnSend);
handler =new Handler();
edtTextMessage = (EditText) findViewById(R.id.edtTextMessage);
linearMessage=(LinearLayout)findViewById(R.id.linearMessage);
socket.connect();
socket.on("message", handlerIncomingMessage);
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String message = edtTextMessage.getText().toString();
sendMessage(message);
}
});
}
public Emitter.Listener handlerIncomingMessage = new Emitter.Listener() {
@Override
public void call(Object... args) {
handler.post(new Runnable() {
@Override
public void run() {
JSONObject jsonObject=(JSONObject)args[0];
String message="";
try {
message=jsonObject.getString("message").toString();
TextView textView=new TextView(getApplicationContext());
textView.setText(message);
textView.setTextSize(18);
layoutParams=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
linearMessage.addView(textView);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
};
private void sendMessage(String message) {
socket.emit("Message", message);
}
}
please guide me.