Javascript 如何从websocket连接保存来自客户端的传入文本文件

Javascript 如何从websocket连接保存来自客户端的传入文本文件,javascript,node.js,html,websocket,Javascript,Node.js,Html,Websocket,我试图在node.js中实现websocket服务器,而不使用任何框架。 从客户端向服务器发送消息工作正常。但现在我尝试将一个文本文件从客户端发送到服务器。我可以使用终端中的console.log查看服务器端的内容。 但是: 如何获取文件信息?(名称、创建/编辑日期等) 如何保存该文件 客户端代码: (function () { 'use strict'; var output, ws; //Display logging information in the docu

我试图在node.js中实现websocket服务器,而不使用任何框架。 从客户端向服务器发送消息工作正常。但现在我尝试将一个文本文件从客户端发送到服务器。我可以使用终端中的console.log查看服务器端的内容。 但是:

  • 如何获取文件信息?(名称、创建/编辑日期等)

  • 如何保存该文件

  • 客户端代码:

    (function () {
        'use strict';
        var output, ws;
    
        //Display logging information in the document
        function log(s) {
            var p = document.createElement("p");
            p.style.wordWrap = "break-word";
            p.textContent = s;
            output.appendChild(p);
    
            //Also log information on the javascript console
            window.console.log(s);
        }
    
        //Send a message on the Websocket
        function sendMessage(msg) {
            console.log(ws.binaryType);
            ws.send(msg);
            console.log("Message sent");
        }
    
        //Initialize WebSocket connection and event handlers
        function setup() {
            output = document.getElementById("output");
            ws = new window.WebSocket("ws://localhost:9999/");
    
            //Listen for the connection open event then call the sendMessage function
            ws.onopen = function () {
                console.log("Connected");
                document.getElementById('fl').onchange = function() {
                    sendMessage(document.querySelector('input[type="file"]').files[0]);
                };
                sendMessage("Hello Galileo!");
            }
    
            //Listen for the close connection event
            ws.onclose = function (e) {
                if(this.readyState == 2){
                    console.log('Connection is closing (Closing Handshake).')
                }
                else if(this.readyState == 3){
                    console.log('Connection closed. Has been closed or could not be opened.')
                }
                else{
                    console.log('Unhandled ReadyState: ',this.readyState);
                }
                console.log("Disconnected: " + 
                           ' reason:' + e.reason + 
                           ' was clean:' + e.wasClean + 
                           ' code:' + e.code);
            }
    
            //Listen for connection errors
            ws.onerror = function (e) {
                console.log("Error: " + e);
            }
    
            //Listen for new messages arriving at the client
            ws.onmessage = function (e) {
                console.log("Message received: " + e.data);
                //Close the socket once one message has arrived
                ws.close();
            }
    
        }
        //Start running the example
        setup();
    
    })();
    
    HTML代码

    
    Websocket回送客户端
    Websocket回送客户端
    
    据我所知,您在服务器端接收的有效负载不包含有关该文件的元数据。我相信
    文件
    对象被视为一个正常的
    Blob
    ,带有一些额外的元数据,而
    ws.send
    只是像
    Blob
    ()一样处理它

    元数据可以使用

    document.querySelector('input[type="file"]').files[0].name
    document.querySelector('input[type="file"]').files[0].size
    document.querySelector('input[type="file"]').files[0].type
    

    然后分别发送。

    我跟踪了服务器和客户端之间的连接。websocket协议发送一个帧,数据通过tcp分块。你知道如何重新组合tcp数据吗?在node.js中,不会发出socket.on“end”事件,因为fin是通过websocket框架发送的,而不是tcp@benu我想你最好单独问这个问题,因为它与这个问题没有什么关系,我也不完全确定你想完成什么。@benu如果你想做的只是检查websocket连接,Chrome现在提供的工具就是这个。