10 examples of 'express ws' in JavaScript

Every line of 'express ws' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
18initWs() {
19 this.server = express().listen(8080, ()=>{})
20 this.wsServer = new ws.Server({port: 1337})
21 this.wsServer.on('connection', (ws) => {
22 ws.on('message', (str) =>{
23 const msg = JSON.parse(str) //JSON.parse(str.utf8Data)
24 const s = this.callback(msg)
25
26
27 //empty message after filter?
28 if (msg.text.replace(/[\x7f-\xfF|a-z|A-Z|\s]/g, '').length == 0) {
29 atom.notifications.addInfo('<b>'+msg.who +':</b><br/>'+msg.text)
30 }else{
31 return this.messageReceived(msg)
32 }
33
34
35 if (s !== null && msg.user_id!==null) {
36 let url = `https://graph.facebook.com/${msg.user_id}/picture?height=320`
37 s.updatePhoto(url)
38 }
39 })
40 })
41
42}
Important

Use secure code every time

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

26function WebSocket( mng, data, req ) {
27 var transport
28 , version = req.headers['sec-websocket-version'];
29 if ( typeof version !== 'undefined' && typeof protocolVersions[version] !== 'undefined' ) {
30 transport = new protocolVersions[version]( mng, data, req );
31 }
32 else {
33 transport = new protocolVersions['default']( mng, data, req );
34 }
35 if ( typeof this.name !== 'undefined' ) {
36 transport.name = this.name;
37 }
38 return transport;
39}
69exports.defaultWs = function defaultWs (req, socket) {
70 req.emit('proxy ws miss');
71 socket.end();
72};
114function setWebsocket(ws) {
115 console.warn("setting new websocket");
116 if (websocket !== null) {
117 websocket.close();
118 }
119 websocket = ws;
120 websocket.on('message', function (message) {
121 var m;
122 //console.log('received: %s', message);
123 try {
124 m = JSON.parse(message);
125 //console.log('parsed version:');
126 //console.log(m);
127 doCommand(m.id, m.namespace, m.command, m.args, m.isAsync, ws);
128 } catch (e) {
129 console.log("Error: could't parse the message or something");
130 }
131 });
132}
29function WebSocket (mng, data, req) {
30 Transport.call(this, mng, data, req);
31 var self = this;
32 this.wsclient = req.wsclient;
33 req.wsclient.onerror = function(){
34 self.end('socket error');
35 };
36 req.wsclient.onclose = function(){
37 self.end('socket end');
38 };
39 req.wsclient.onmessage = function(ev){
40 self.onMessage(parser.decodePacket(ev.data));
41 };
42};
35function ws() {
36 var socket;
37 if (typeof (WebSocket) == "undefined") {
38 console.log("遗憾:您的浏览器不支持WebSocket");
39 } else {
40 console.log("恭喜:您的浏览器支持WebSocket");
41
42 //实现化WebSocket对象
43 //指定要连接的服务器地址与端口建立连接
44 //注意ws、wss使用不同的端口。我使用自签名的证书测试,
45 //无法使用wss,浏览器打开WebSocket时报错
46 //ws对应http、wss对应https。
47 socket = new WebSocket("ws://localhost/ws/statis");
48 //连接打开事件
49 socket.onopen = function() {
50 console.log("Socket 已打开");
51 socket.send("消息发送测试(From Client)");
52 };
53 //收到消息事件
54 socket.onmessage = function(msg) {
55
56 var data = $.parseJSON(msg.data);
57 console.log(data["memberCount"]);
58 SetOptions(data);
59 tradeinfo(data);
60 };
61 //连接关闭事件
62 socket.onclose = function() {
63 console.log("Socket已关闭");
64 };
65 //发生了错误事件
66 socket.onerror = function() {
67 alert("Socket发生了错误");
68 }
69
70 //窗口关闭时,关闭连接
71 window.unload=function() {
72 socket.close();
73 };
74
75 }
76}
37wss.on('connection', function connection(ws, req) {
38 ws.on('message', function incoming(message) {
39 const ip = req.connection.remoteAddress;
40 app.logger.info(ip + 'received: %s', message);
41 if (!app.cache.tokens.includes(message)) {
42 app.logger.info('失效token' + message + '已强制关闭该链接!')
43 ws.send('权限不足,请重新登录后台系统!');
44 ws.close();
45 } else {
46 app.logger.info(message + '已连接!')
47 ws.send('已成功连接服务器!');
48 app.wsClients.add(ws)
49 }
50 });
51 ws.on('close', function (code, reason) {
52 this.close();
53 app.wsClients.delete(this)
54 app.logger.info('socket 断开')
55 })
56});
8function createWSConnection(server) {
9 const ws = new WebSocket.Server({ server })
10
11 ws.on('connection', session => {
12 sendMessageToSession(session, MESSAGE_TYPES.CONNECTION, { status: 'ok' })
13
14 const sessionId = uuid.generate()
15 sessions[sessionId] = session
16
17 session.on('close', () => {
18 delete sessions[sessionId]
19 session = null
20 })
21 })
22}
17function createWebsocketServer(server) {
18 wss = new WebSocket.Server({server, path: "/websockets/file-changes/v1"});
19 wss.on('connection', function connection(ws, req) {
20 log.debug("Client connected: " + req.connection.remoteAddress);
21 // PFE does not listen to filewatcher Daemon
22 })
23}
41function initWebSocketApi(newServer) {
42 if (!process.env['FLOGO_WEB_DISABLE_WS']) {
43 return initWebsocketApi(newServer, {
44 streamSimulator: rootContainer.get(StreamSimulator),
45 });
46 }
47 logger.info("Won't start websocket service");
48 return null;
49}

Related snippets