【问题标题】:samsung device not connect to other device using web socket三星设备无法使用网络套接字连接到其他设备
【发布时间】:2022-11-06 18:12:53
【问题描述】:

现在我正在尝试使用三星 S6 Lite 连接其他设备。在这里,我在连接到它尝试连接的其他设备时遇到问题,几秒钟后,OnClose 方法将在我的 Web 套接字侦听器中调用。

我在三星 S7、三星 S5 和模拟器上试过同样的东西,它工作正常。只有我在三星 S6(操作系统:Android 11)中遇到了连接问题。

节点.js

const http = require('http');
const WebSocketServer = require('websocket').server;

const server = http.createServer();
server.listen(8080);

const wsServer = new WebSocketServer({
    httpServer: server
});

wsServer.on('request', function(request) {
    const connection = request.accept(null, request.origin);

    console.log('Server Connected')

    connection.on('message', function(message) {
      console.log('Received Message:', message.utf8Data);
      //connection.sendUTF('Hi this is WebSocket server!');
      connection.sendUTF('Socket server conne');
    });

    connection.on('close', function(reasonCode, description) {
        console.log('Client has disconnected.',description.utf8Data);
    });

    connection.on('error', function(error){
        console.log('Client has error',error.utf8Data);
    });

    connection.on('frame', function(frame){
        console.log('Client has frame');
    });
    
});

wsServer.on('connect',function(connection){
    console.log('Server connection status: ');

    connection.on('message', function(message){
        console.log('Server Received Message:', message.utf8Data);
    });

});

安卓连接代码:

public void test() {
        try {
            URI uri;
            try {
                uri = new URI("ws://localhost:8080/test");
            } catch (URISyntaxException e) {
                e.printStackTrace();
                return;
            }
            String myAndroidDeviceId = Settings.Secure.getString(getApplicationContext().getContentResolver(), ANDROID_ID);
            Map<String, String> stringStringMap = new HashMap<>();
            stringStringMap.put("device", "device," + myAndroidDeviceId);
           
            String temp[] = {"device:device"};
            cc = new WebSocketClient(uri) {
                @Override
                public void onOpen(ServerHandshake serverHandshake) {
                    String str = android.os.Build.MODEL;
                    String record = "Device Name : " + str;
                    // cc.send(String.valueOf(record));
                    cc.send(record);
                    System.out.println("Address" + cc.getRemoteSocketAddress());
                    Log.i("Websocket", "Opened");
                    System.out.println("SocketIPAddress--->Opened--->");
                }

                @Override
                public void onMessage(String s) {
                    final String message = s;
                    System.out.println(message);
                    System.out.println("SocketIPAddress--->onMessage--->" + s);
                    updateUI(message);
                }

                @Override
                public void onClose(int i, String s, boolean b) {
//                  Log.i("Websocket", "Closed " + s);
                    System.out.println("SocketIPAddress--->onClose--->" + s);
                    //updateUI("Disconnected");
                }

                @Override
                public void onError(final Exception e) {
                    System.out.println("SocketIPAddress--->onError--->" + e.getMessage());
                }

            };

            
            cc.connect();
            
        } catch (Exception ex) {
            Log.i("ErrorMsg", ex.getMessage());
        }
    }

请任何人帮助解决此问题。谢谢

【问题讨论】:

    标签: android node.js connection java-websocket


    【解决方案1】:

    你能解决这个问题吗?我有同样的问题。谢谢

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-21
    • 1970-01-01
    • 2019-10-09
    • 2021-02-27
    • 2016-05-18
    • 2020-10-23
    相关资源
    最近更新 更多