How to use the ws.createServer function in ws

To help you get started, we’ve selected a few ws examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github penk / mkxpud / skeleton / overwrite / usr / share / plate / chrome / content / daemon.js View on Github external
var ws = require("ws"), 
sys = require('sys'), 
fs = require('fs'),
Inotify = require('inotify').Inotify;

var inotify = new Inotify();
var server = ws.createServer();

var callback = function(event) {
    var mask = event.mask;
	if(mask & Inotify.IN_CLOSE_WRITE) {
		// file was modified

		fs.createReadStream("/tmp/xpudctrl", {
		    'flags': 'r',
		    'encoding': 'UTF-8',
		    'mode': 0666,
		    'bufferSize': 4 * 1024
		}).addListener("data", function(data){
			data = data.toString();
			console.log(data);
			server.broadcast(data);
		});
github marinho / node-jobs / node.jobs / websocket_server.js View on Github external
store.open(function(error, db){

        // Create application and set its middlewares
        var server = ws.createServer();

        server.addListener("listening", function(){
          sys.log("Listening for connections.");
        });

        // Handle WebSocket Requests
        server.addListener("connection", function(conn){
          //conn.send("Connection: "+conn.id);

          conn.addListener("message", function(message){
            var msg = JSON.parse(message);
            sys.log(msg.method);

            if (msg.method !== undefined) {
                methods[msg.method](store, db, conn, msg);
            }
github James-Yu / LaTeX-Workshop / obsolete / preview.ts View on Github external
response.writeHead(200, { 'Content-Type': contentType });
                    response.end(content, 'utf-8');
                }
            });
        } else {
            find_main_document();
            var file = tex2PdfFile(latex_data.main_document)
            var stat = fs.statSync(file);
            response.writeHead(200, {
                'Content-Type': 'application/pdf',
                'Content-Length': stat.size
            });
            fs.createReadStream(file).pipe(response)
        }
    });
    ws_server = ws.createServer({server: http_server});
    listening = new Promise((c, e) => http_server.listen(0, "localhost", undefined, err => {
        if (err)
            e(err)
        else {
            var {address, port} = http_server.address();
            in_browser_url = `http://${address}:${port}/web/viewer.html?file=pdf-preview`
            c()
        }
    }));
    ws_server.on("connection", ws => {
        ws.on("message", onClientMessage);
        ws.on("close", onClientClose);
    });
}
github ajshort / vscode-latex-preview / src / document-provider.ts View on Github external
constructor(private context: vscode.ExtensionContext) {
    this.http = http.createServer();
    this.server = ws.createServer({ server: this.http });

    this.listening = new Promise((c, e) => {
      this.http.listen(0, "localhost", undefined, err => err ? e(err) : c());
    });

    this.server.on("connection", client => {
      client.on("message", this.onClientMessage.bind(this, client));
      client.on("close", this.onClientClose.bind(this, client));
    });

    this.diagnostics = vscode.languages.createDiagnosticCollection("LaTeX Preview");
    this.output = vscode.window.createOutputChannel("LaTeX Preview");
  }
github amokjs / amok / lib / multiplex.js View on Github external
response.end(JSON.stringify(targets, undefined, 2));
          });
        });

        upstreamRequest.end();
      } else {
        upstreamRequest.on('response', upstreamResponse => {
          upstreamResponse.pipe(response);
        });

        upstreamRequest.end();
      }
    });

    let socket = ws.createServer({ server: server });
    socket.on('connection', connection => {
      let upstreamUrl = url.format({
        protocol: 'ws',
        slashes: true,
        hostname: debugHost,
        port: debugPort,
        pathname: connection.upgradeReq.url,
      });

      if (!upstream[upstreamUrl]) {
        let upstreamConnection = ws.createConnection(upstreamUrl);
        upstream[upstreamUrl] = {
          localId: 0,
          connection: upstreamConnection,
          connections: [connection],
          mappings: {},
github ionic-team / ionic-app-scripts / src / dev-server / ws-server.ts View on Github external
export function createWsServer({ port }: WSOptions) {
  const wss = ws.createServer({ port });
  let subscribers: Function[] = [];

  const broadcast = (data: any): void => {

    wss.clients.forEach((client: any) => {
      if (client.readyState === ws.OPEN) {
        client.sendJson(data);
      }
    });
  };

  const onMessage = (fn: Function): void => {
    subscribers.push(fn);
  };

  wss.on('connection', (client: any) => {
github waratuman / flight-stream / server.js View on Github external
response.writeHead(200, {'Content-Type': mime.lookup(filename)});
		fs.createReadStream(filename, {
			'flags': 'r',
			'encoding': 'binary',
			'mode': 0666,
			'bufferSize': 4 * 1024
		}).addListener("data", function(chunk) {
			response.write(chunk, 'binary');
		}).addListener("close",function() {
			response.end();
		});
	});
});

var server = ws.createServer({}, httpServer);

db.subscribeTo("flight_stream", function(channel, message, pattern) {
	try { var flight = JSON.parse(message); }
	catch (SyntaxError) { return false; }

	if ( flight.origin.iata == "BOS" || flight.destination.iata == "BOS") {
		server.broadcast(message);
	}
});

server.listen(8000);
github turbolent / ralph / core / ralph / repl / interactor.js View on Github external
($module)['start-server'] = function B112(interactor__113, port__114) {
        var server__115 = B14['createServer']({ 'port': port__114 }, B8['curry'](($module)['handle-connection'], interactor__113));
        return(B8['get-setter'](interactor__113, 'server', server__115));
    };
    B8['%annotate-function'](($module)['start-server'], 'start-server', false);
github creationix / howtonode.org / articles / redis-pubsub / server.js View on Github external
response.writeHead(200, {'Content-Type': mime.lookup(filename)});
        fs.createReadStream(filename, {
            'flags': 'r',
            'encoding': 'binary',
            'mode': 0666,
            'bufferSize': 4 * 1024
        }).addListener("data", function(chunk) {
            response.write(chunk, 'binary');
        }).addListener("close",function() {
            response.end();
        });
    });
});


var server = ws.createServer({server: httpServer});

server.listen(8000);

ws

Simple to use, blazing fast and thoroughly tested websocket client and server for Node.js

MIT
Latest version published 6 months ago

Package Health Score

89 / 100
Full package analysis