I am created chat application using Websocket
.In this I am uploading image to room
. When I upload Image I will end up with following error and it creates new connection for the all the client the Room. Error coming from below function.
connection.on("error",function(err){
console.log('error',err.code)
console.log(err)
})
following log message
server listening to port 3001
new user connected hi #user 1 (room1) he is going to upload image
new user connected hi #user 2(room1)
error ECONNRESET
Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:209:20) {
errno: -4077,
code: 'ECONNRESET',
syscall: 'read'
}
user disconnected Socket Error: read ECONNRESET # user1(room1) disconnected
error ECONNRESET
Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:209:20) {
errno: -4077,
code: 'ECONNRESET',
syscall: 'read'
}
user disconnected Socket Error: read ECONNRESET #user2(room1) disconnected
new user connected hi #user 1 (room1)
new user connected hi #user 2(room1)
two clients are listening to port 3000 : (react)
Client Code : App.js
constructor(){
super()
this.connection = null;
this.appName = "Chat-App";
this.SERVER = "http://localhost:3001/";
}
componentDidMount(){
this.connection = new WebSocket('ws://localhost:3001');
}
handleImageUpload = async(event,photo)=>{
event.preventDefault()
let formData = new FormData();
formData.append("photo", photo);
fetch(this.SERVER+'uploads', {method: "POST", body: formData})
.then((response)=>{
console.log('response from server',response)
this.connection.send(this.createJsonString("image_upload",photo.name,this.state.activeRoom));
})
Server side
const UPLOAD_LOC = './../client/public/'
var storage = multer.diskStorage({
destination: function(req,file,callback) {
callback(null, UPLOAD_LOC);
},
filename: function(req,file,callback) {
callback(null,file.originalname);
}
})
var upload = multer({ storage: storage })
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3000');
res.setHeader('Access-Control-Allow-Methods', 'POST');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
})
var server = require('http').createServer(app);
app.post('/uploads',upload.single("photo"),function(req,res){
res.send({error:'false',message:'image uploaded'});
})
server side event handler
wsServer.on('request', function(request) {
console.log('new user connected','hi')
var connection = request.accept(null, request.origin);
connection.on('message', function(message) {
const json = JSON.parse(message.utf8Data)
let userId = json.userId;
let body = json.body;
switch(json.meta){
case 'image_upload':
fs.readFile(UPLOAD_LOC+body,function(err,buf){ //here body contains file name
if(!err){
userList = getRoomUserList(json.roomName);//get the all the user from the room
for(i = 0;i<userList.length;i++){
let user = userList[i];
getSocketId(user).send(createJsonString('image_upload',user,buf.toString('base64'),json.roomName))
}
}else{
console.log('is this error',err)
}
})
break;
}
Please help me ... Thanks in advance ..!
I will answer my own question . Here I what did is I am uploading image to react default folder public
this is the problem.
I changed upload directory now it is working fine.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.