如何将socket.io-stream与electron.js一起使用?

时间:2018-07-07 16:11:28

标签: node.js angular socket.io streaming electron

我用电子开发了一个应用程序。我将此应用程序用作文件传输服务。但是当我使用socket.io-stream时,会出现一些问题。

  • 服务器端工作正常。它接受来自另一个的流 快递项目。
  • fs = this.electronService.remote.require('fs');中,我认为问题是“烟斗”,但我该怎么办?

请帮助我。

  

带有Express的服务器端代码

var express = require('express');  
var app = express();  
var server = require('http').createServer(app);  
var ios = require('socket.io')(server);
var ss=require('socket.io-stream');
var fs=require('fs');
var redisAdapter = require('socket.io-redis');
ios.adapter(redisAdapter({ host: '127.0.0.1', port: 6379 }));
var io = ios.of('/test');

app.use(express.static(__dirname + '/node_modules'));  
app.get('/', function(req, res,next) {  
  res.send('Hello 3000!');
});

io.on('connection', function(socket) {  
   let room=socket.handshake.query.room;
   socket.join(room);

   ss(socket).on('client2serverfile', function(stream) {
      stream.pipe(fs.createWriteStream('c:/temp/X1.txt'));
      stream.on('end',()=>{
         console.log('File saved'); 
      })
   });
});

server.listen(3000, () => console.log('Example app listening on port 3000!'))
  

电子和角度为5的客户端代码

import { Component,OnInit } from '@angular/core';
import {ElectronService} from 'ngx-electron';
import * as io from 'socket.io-client';
import * as ss from 'socket.io-stream';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})

export class AppComponent {
   constructor(private electronService: ElectronService){};
   socket = io('http://localhost:3000'+ '/test',{query:"room=myroom"});
   fs = this.electronService.remote.require('fs');

   streamTransfer(){//triger button
     let stream= ss.createStream();
     ss(this.socket).emit('client2serverfile',stream);
     this.electronService.fs.createReadStream('c:/temp/x2.txt').pipe(stream);
     stream.on('end',()=>{
        console.log('OK')
     })
   }
}

请忽略代码中的错别字。 运行代码时,出现以下错误。

events.js:170 Uncaught TypeError: this.on is not a function
    at webpackJsonp../node_modules/events/events.js.EventEmitter.once(events.js:170)
    at CallbacksRegistry.apply (C:\123proje\123NG\123YA\YA-TEST\testangular\node_modules\electron\dist\resources\electron.asar\common\api\callbacks-registry.js:47)
    at EventEmitter.ipcRenderer.on (C:\123proje\123NG\123YA\YA-TEST\testangular\node_modules\electron\dist\resources\electron.asar\renderer\api\remote.js:261)
    at emitThree (events.js:136)
    at EventEmitter.emit (events.js:217)
webpackJsonp../node_modules/events/events.js.EventEmitter.once @events.js:170
apply @ C:\123proje\123NG\123YA\YA-TEST\testangular\node_modules\electron\dist\resources\electron.asar\common\api\callbacks-registry.js:47
ipcRenderer.on @ C:\123proje\123NG\123YA\YA-TEST\testangular\node_modules\electron\dist\resources\electron.asar\renderer\api\remote.js:261
emitThree @ events.js:136
emit @ events.js:217
events.js:170 Uncaught TypeError: this.on is not a function
  at webpackJsonp../node_modules/events/events.js.EventEmitter.once (events.js:170)
  at CallbacksRegistry.apply (C:\123proje\123NG\123YA\YA-TEST\testangular\node_modules\electron\dist\resources\electron.asar\common\api\callbacks-registry.js:47)
  at EventEmitter.ipcRenderer.on (C:\123proje\123NG\123YA\YA-TEST\testangular\node_modules\electron\dist\resources\electron.asar\renderer\api\remote.js:261)
  at emitThree (events.js:136)
  at EventEmitter.emit (events.js:217)
webpackJsonp../node_modules/events/events.js.EventEmitter.once @events.js:170
apply @ C:\123proje\123NG\123YA\YA-TEST\testangular\node_modules\electron\dist\resources\electron.asar\common\api\callbacks-registry.js:47
ipcRenderer.on @ C:\123proje\123NG\123YA\YA-TEST\testangular\node_modules\electron\dist\resources\electron.asar\renderer\api\remote.js:261
emitThree @ events.js:136
emit @ events.js:217
_stream_writable.js:317 Uncaught TypeError: Cannot read property 'objectMode' of undefined
  at webpackJsonp../node_modules/readable-stream/lib/_stream_writable.js.Writable.write (_stream_writable.js:317)
  at CallbacksRegistry.apply (C:\123proje\123NG\123YA\YA-TEST\testangular\node_modules\electron\dist\resources\electron.asar\common\api\callbacks-registry.js:47)
  at EventEmitter.ipcRenderer.on (C:\123proje\123NG\123YA\YA-TEST\testangular\node_modules\electron\dist\resources\electron.asar\renderer\api\remote.js:261)
  at emitThree (events.js:136)
  at EventEmitter.emit (events.js:217)
webpackJsonp../node_modules/readable-stream/lib/_stream_writable.js.Writable.write @ _stream_writable.js:317
apply @ C:\123proje\123NG\123YA\YA-TEST\testangular\node_modules\electron\dist\resources\electron.asar\common\api\callbacks-registry.js:47
ipcRenderer.on @ C:\123proje\123NG\123YA\YA-TEST\testangular\node_modules\electron\dist\resources\electron.asar\renderer\api\remote.js:261
emitThree @ events.js:136
emit @ events.js:217
_stream_writable.js:588 Uncaught TypeError: Cannot read property 'corked' of undefined
  at webpackJsonp../node_modules/readable-stream/lib/_stream_writable.js.Writable.end (_stream_writable.js:588)
  at CallbacksRegistry.apply (C:\123proje\123NG\123YA\YA-TEST\testangular\node_modules\electron\dist\resources\electron.asar\common\api\callbacks-registry.js:47)
  at EventEmitter.ipcRenderer.on (C:\123proje\123NG\123YA\YA-TEST\testangular\node_modules\electron\dist\resources\electron.asar\renderer\api\remote.js:261)
  at emitThree (events.js:136)
  at EventEmitter.emit (events.js:217)

0 个答案:

没有答案