gulp-ws-sender
Advanced tools
Comparing version 1.1.1 to 1.2.0
{ | ||
"name": "gulp-ws-sender", | ||
"version": "1.1.1", | ||
"description": "start the websocket server and send your js and css to it ^^", | ||
"version": "1.2.0", | ||
"description": "For live injection of your script and styles on any page", | ||
"main": "index.js", | ||
@@ -21,3 +21,11 @@ "scripts": { | ||
"url": "https://github.com/bercly0b/gulp-ws-sender" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^5.10.0", | ||
"eslint-config-standard": "^12.0.0", | ||
"eslint-plugin-import": "^2.14.0", | ||
"eslint-plugin-node": "^8.0.0", | ||
"eslint-plugin-promise": "^4.0.1", | ||
"eslint-plugin-standard": "^4.0.0" | ||
} | ||
} |
## usage | ||
```sh | ||
const gulp = require('gulp') | ||
const sender = require('gulp-ws-sender')(9999) // port | ||
gulp.task('styles', () => { | ||
const port = 9999 | ||
const sender = require('gulp-ws-sender')(port) | ||
// ... | ||
gulp.task('style', () => { | ||
return gulp.src('src/**/*.css') | ||
.pipe(sender({ type: 'css' })) // or type: 'js' | ||
.pipe(gulp.dest('public')) | ||
// ... | ||
.pipe(sender({ type: 'css' })) | ||
.pipe(gulp.dest('build')) | ||
}) | ||
gulp.task('script', () => { | ||
return gulp.src('src/**/*.js') | ||
// ... | ||
.pipe(sender({ type: 'js' })) | ||
.pipe(gulp.dest('build')) | ||
}) | ||
// ... | ||
``` | ||
For live injection of your script and styles on any page. Use with [chrome extention](https://github.com/bercly0b/injector-chrome-extension) | ||
For live injection of your script and styles on any page. Use with [chrome extention](https://chrome.google.com/webstore/detail/injector/fopkjckkihccjckhmikeclmkghlipbil?hl=en-GB) |
@@ -1,18 +0,13 @@ | ||
const PluginError = require('plugin-error'); | ||
const PluginError = require('plugin-error') | ||
const through = require('through2') | ||
const WebSocket = require('ws') | ||
const server = require('./server') | ||
const Server = require('./server') | ||
const getMessage = require('./helper').getClientMessage | ||
module.exports = port => { | ||
const app = new Server(port) | ||
server(port) | ||
const ws = new WebSocket('ws://localhost:' + port) | ||
return options => { | ||
const { type } = options | ||
return through.obj(function(file, enc, cb) { | ||
if (file.isNull()) { | ||
@@ -30,3 +25,3 @@ cb(null, file) | ||
const prefix = type === 'js' ? 'js' : 'st' | ||
ws.send(prefix + file.contents.toString()) | ||
app.send(prefix + file.contents.toString()) | ||
@@ -33,0 +28,0 @@ file.contents = Buffer.from(file.contents) |
const WebSocket = require('ws') | ||
const getMessage = require('./helper').getServerMessage | ||
module.exports = port => { | ||
module.exports = class Server { | ||
constructor(port) { | ||
this.clients = {} | ||
this._init(port) | ||
} | ||
const clients = {} | ||
let needHandler = true | ||
_init(port) { | ||
this.server = new WebSocket.Server({ port }) | ||
const webSocketServer = new WebSocket.Server({ port }) | ||
this.server.on('connection', ws => { | ||
ws.id = Math.random().toString(36).substring(5) | ||
this.clients[ws.id] = ws | ||
console.log(getMessage('Connected', `browser (id: ${ws.id})`)) | ||
webSocketServer.on('connection', ws => { | ||
ws.on('close', () => { | ||
delete this.clients[ws.id] | ||
console.log(getMessage('Disconnected', `browser (id: ${ws.id})`)) | ||
}) | ||
}) | ||
const client = clients.sender ? 'browser' : 'sender' | ||
clients[client] = ws | ||
console.log(getMessage('Connected', client)) | ||
ws.on('close', () => { | ||
delete clients[client] | ||
console.log(getMessage('Disconnected', client)) | ||
console.log(getMessage('Server is waiting for connection on port', port)) | ||
} | ||
send(data) { | ||
Object.values(this.clients).forEach(client => { | ||
if (client.readyState === WebSocket.OPEN) client.send(data) | ||
}) | ||
if (needHandler && clients.sender) { | ||
needHandler = false | ||
clients.sender.on('message', message => { | ||
// console.log(getMessage('Received message from', client), `: ${message}`) | ||
clients.browser && clients.browser.send(message) | ||
}) | ||
} | ||
}) | ||
} | ||
} |
3787
7
74
28
6