gpt4 book ai didi

android - 我可以通过 native java.net.Socket 连接 Android 客户端与使用 socket.io 的 Node js 服务器吗?

转载 作者:太空宇宙 更新时间:2023-11-03 22:24:14 24 4
gpt4 key购买 nike

我使用node.js 和socket.io 为聊天应用程序构建了一个服务器,并且我想从使用 native java.net.Socket 的Android 客户端应用程序连接到该服务器。我可以做吗?

最佳答案

在这里我找到了一个效果很好的解决方案。此代码部分用于服务器端套接字。

var net = require('net');
var sockets = [];

var svr = net.createServer(function(sock) {
console.log('Connected: ' + sock.remoteAddress + ':' + sock.remotePort);
sockets.push(sock);

sock.write('Welcome to the server!\n');

sock.on('data', function(data) {
for (var i=0; i<sockets.length ; i++) {
if (sockets[i] != sock) {
if (sockets[i]) {
sockets[i].write(data);
}
}
}
});

sock.on('end', function() {
console.log('Disconnected: ' + sock.remoteAddress + ':' + sock.remotePort);
var idx = sockets.indexOf(sock);
if (idx != -1) {
delete sockets[idx];
}
});
});

var svraddr = '192.168.0.8';
var svrport = 1234;

svr.listen(svrport, svraddr);
console.log('Server Created at ' + svraddr + ':' + svrport + '\n');

Android客户端代码如下:通过Android客户端连接到服务器给定的IP和端口。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;

public class Client {
private Socket socket;
private OutputStream socketOutput;
private BufferedReader socketInput;

private String ip;
private int port;
private ClientCallback listener=null;

public Client(String ip, int port){
this.ip=ip;
this.port=port;
}

public void connect(){
new Thread(new Runnable() {
@Override
public void run() {
socket = new Socket();
InetSocketAddress socketAddress = new InetSocketAddress(ip, port);
try {
socket.connect(socketAddress);
socketOutput = socket.getOutputStream();
socketInput = new BufferedReader(new InputStreamReader(socket.getInputStream()));

new ReceiveThread().start();

if(listener!=null)
listener.onConnect(socket);
} catch (IOException e) {
if(listener!=null)
listener.onConnectError(socket, e.getMessage());
}
}
}).start();
}

public void disconnect(){
try {
socket.close();
} catch (IOException e) {
if(listener!=null)
listener.onDisconnect(socket, e.getMessage());
}
}

public void send(String message){
try {
socketOutput.write(message.getBytes());
} catch (IOException e) {
if(listener!=null)
listener.onDisconnect(socket, e.getMessage());
}
}

private class ReceiveThread extends Thread implements Runnable{
public void run(){
String message;
try {
while((message = socketInput.readLine()) != null) { // each line must end with a \n to be received
if(listener!=null)
listener.onMessage(message);
}
} catch (IOException e) {
if(listener!=null)
listener.onDisconnect(socket, e.getMessage());
}
}
}

public void setClientCallback(ClientCallback listener){
this.listener=listener;
}

public void removeClientCallback(){
this.listener=null;
}

public interface ClientCallback {
void onMessage(String message);
void onConnect(Socket socket);
void onDisconnect(Socket socket, String message);
void onConnectError(Socket socket, String message);
}
}

主要 Activity 是:

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

Client socket = new Client("192.168.0.8", 1234);
socket.setClientCallback(new Client.ClientCallback () {
@Override
public void onMessage(String message) {
}

@Override
public void onConnect(Socket socket) {
socket.send("Hello World!\n");
socket.disconnect();
}

@Override
public void onDisconnect(Socket socket, String message) {
}

@Override
public void onConnectError(Socket socket, String message) {
}
});

socket.connect();
}
}

关于android - 我可以通过 native java.net.Socket 连接 Android 客户端与使用 socket.io 的 Node js 服务器吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49023074/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com