I have created a websocket connection for the purpose of serving webrtc requests/answers (client):
export function webSocketConnect(room_code, action, username){
let scheme = "ws";
/* If the protocol is https you must use web socket secured */
if(document.location.protocol === "https:"){
scheme += "s";
}
getServerURL = createGetterForParam(hostname + ":" + server_port);
let websocketScheme = scheme + "://" + getServerURL.get();
log("Server URL is: " + websocketScheme);
try{
web_socket_connection = new WebSocket(websocketScheme, "json");
}catch(err){
throw new Error("couldn't connect you to the server");
}
//assign event handlers
...
}
This connects to a node js server(server):
try{
http_s_server = https.createServer(
https_options,
handleHttpsRequest
);
log("Created a https server");
}catch(err){...}
http_s_server.listen(port, function(){
log("Https server is listening on port: " + port);
});
web_socket_server = new WebSocketServer({
httpServer: http_s_server,
autoAcceptConnections: false,
});
web_socket_server.on('request',async function(request) {
log("request received");
let connection = request.accept("json", request.origin);
assignConnectionHandlers(connection);
...
}
I want to upload a file to the server and I'm using a post request(getting the file from <input type="file"...>
,client):
let new_filename = getUsername.get() + "_" + getClientID.get() + "_" + getRoomCode.get() + "_" + file.name;
let formdata = new FormData();
formdata.append("file", file, new_filename);
fetch(window.location.protocol + "//" + getServerURL.get() + "/Files", {
method: "POST",
body: formdata
})
.then(response => {
// handle the response here
log(JSON.stringify(response));
})
.then(data => {
// handle the data here
})
.catch(err => {
log(err);
});
And in the server using the formidable
framework:
function handleHttpsRequest(request, response){
let processed_url = request.url.split("?")[0];
let params = request.url.split("?")[1];
if (request.method === 'POST' && processed_url === '/Files') {
handleSendFile(request, response);
}
}
function handleSendFile(request, response){
var form = formidable.IncomingForm();
form.on('fileBegin', function(name, file){
file.path = localFilePath + file.name;
});
form.on('file', function(name,file){
log('Uploaded file: ' + file.name);
});
form.parse(request);
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end();
}
The html where I'm getting the file:
<div class="chat-input-icon">
<label for="file-input">
<img id="file-input-image" src="../images/open-explorer.png" alt="File icon">
</label>
</div>
<input type="file" id="file-input" name="file_input" style="display:none;">
The file is successfully saved into the desired directory in the server, but the websocket connection always crashes and I'm getting a:
Error: read ECONNRESET
I have also tried using multiple versions for formidable and nothing seems to work.
I have also tried doing this based on a more standard form and then handling with onsubmit
-> event.preventDefault()
:
<form action="https://localhost:62000/Files" id="upload-file-form" enctype="multipart/form-data" method="post">
<div>Text field title: <input type="text" name="title" /></div>
<div>File: <input type="file" name="multipleFiles" /></div>
<input type="submit" value="Upload" />
</form>
Does anyone know a fix? (Any better implementation is accepted)