From dd04d74769e1859d571de7c4f2e88cb85a227ab9 Mon Sep 17 00:00:00 2001 From: Kai Koehne Date: Mon, 28 Aug 2017 16:31:05 +0200 Subject: Examples: beautify JavaScript snippets Change-Id: I3499bfa49e79ed3664d020f7de0e691afeb15a86 Reviewed-by: Milian Wolff --- .../webchannel/chatclient-html/chatclient.html | 24 ++++----- .../webchannel/chatclient-qml/qmlchatclient.qml | 58 +++++++++++----------- examples/webchannel/nodejs/chatclient.js | 13 +++-- examples/webchannel/qwclient/qwclient.js | 27 +++++----- examples/webchannel/standalone/index.html | 12 ++--- 5 files changed, 69 insertions(+), 65 deletions(-) diff --git a/examples/webchannel/chatclient-html/chatclient.html b/examples/webchannel/chatclient-html/chatclient.html index b392282..d3b36d8 100644 --- a/examples/webchannel/chatclient-html/chatclient.html +++ b/examples/webchannel/chatclient-html/chatclient.html @@ -21,16 +21,13 @@ window.onload = function() { var socket = new WebSocket(wsUri); - socket.onclose = function() - { + socket.onclose = function() { console.error("web channel closed"); }; - socket.onerror = function(error) - { + socket.onerror = function(error) { console.error("web channel error: " + error); }; - socket.onopen = function() - { + socket.onopen = function() { window.channel = new QWebChannel(socket, function(channel) { //connect to the changed signal of a property channel.objects.chatserver.userListChanged.connect(function() { @@ -42,11 +39,11 @@ }); //connect to a signal channel.objects.chatserver.newMessage.connect(function(time, user, message) { - $('#chat').append("[" + time + "] " + user + ": " + message + '
'); + $('#chat').append("[" + time + "] " + user + ": " + message + '
'); }); //connect to a signal channel.objects.chatserver.keepAlive.connect(function(args) { - if(window.loggedin) { + if (window.loggedin) { //call a method channel.objects.chatserver.keepAliveResponse($('#loginname').val()) console.log("sent alive"); @@ -74,11 +71,12 @@ @@ -110,10 +107,11 @@ diff --git a/examples/webchannel/chatclient-qml/qmlchatclient.qml b/examples/webchannel/chatclient-qml/qmlchatclient.qml index abb0ea9..ebca1eb 100644 --- a/examples/webchannel/chatclient-qml/qmlchatclient.qml +++ b/examples/webchannel/chatclient-qml/qmlchatclient.qml @@ -80,34 +80,36 @@ ApplicationWindow { } property var onmessage; - onStatusChanged: if (socket.status == WebSocket.Error) { - console.error("Error: " + socket.errorString) - } else if (socket.status == WebSocket.Closed) { - messageBox.text += "\nSocket closed" - } else if (socket.status == WebSocket.Open) { - //open the webchannel with the socket as transport - new WebChannel.QWebChannel(socket, function(ch) { - root.channel = ch; - - //connect to the changed signal of the userList property - ch.objects.chatserver.userListChanged.connect(function(args) { - userlist.text = ''; - ch.objects.chatserver.userList.forEach(function(user) { - userlist.text += user + '\n'; - }); - }); - //connect to the newMessage signal - ch.objects.chatserver.newMessage.connect(function(time, user, message) { - chat.text = chat.text + "[" + time + "] " + user + ": " + message + '\n'; - }); - //connect to the keep alive signal - ch.objects.chatserver.keepAlive.connect(function(args) { - if (loginName.text !== '') - //and call the keep alive response method as an answer - ch.objects.chatserver.keepAliveResponse(loginName.text) - }); - }); - } + onStatusChanged: { + if (socket.status === WebSocket.Error) { + console.error("Error: " + socket.errorString); + } else if (socket.status === WebSocket.Closed) { + messageBox.text += "\nSocket closed"; + } else if (socket.status === WebSocket.Open) { + //open the webchannel with the socket as transport + new WebChannel.QWebChannel(socket, function(ch) { + root.channel = ch; + + //connect to the changed signal of the userList property + ch.objects.chatserver.userListChanged.connect(function(args) { + userlist.text = ''; + ch.objects.chatserver.userList.forEach(function(user) { + userlist.text += user + '\n'; + }); + }); + //connect to the newMessage signal + ch.objects.chatserver.newMessage.connect(function(time, user, message) { + chat.text = chat.text + "[" + time + "] " + user + ": " + message + '\n'; + }); + //connect to the keep alive signal + ch.objects.chatserver.keepAlive.connect(function(args) { + if (loginName.text !== '') + //and call the keep alive response method as an answer + ch.objects.chatserver.keepAliveResponse(loginName.text); + }); + }); + } + } } GridLayout { diff --git a/examples/webchannel/nodejs/chatclient.js b/examples/webchannel/nodejs/chatclient.js index 5e458f2..76b620c 100644 --- a/examples/webchannel/nodejs/chatclient.js +++ b/examples/webchannel/nodejs/chatclient.js @@ -91,7 +91,10 @@ var createWebChannel = function(transport, rlif) { console.log(' << ' + message); rlif.prompt(); // Go to end of existing input if any - rlif.write(null, {ctrl: true, name: 'e'}) + rlif.write(null, { + ctrl: true, + name: 'e' + }) }); rlif.on('line', function(line) { @@ -118,7 +121,9 @@ socket.on('open', function(event) { var transport = { // We cant't do 'send: socket.send' here // because 'send' wouldn't be bound to 'socket' - send: function(data) {socket.send(data)} + send: function(data) { + socket.send(data) + } }; createWebChannel(transport, createReadlineInterface()); @@ -131,12 +136,12 @@ socket.on('open', function(event) { }); }); -socket.on('error', function (error) { +socket.on('error', function(error) { console.log('Connection error: ' + error.message); process.exit(1); }); -socket.on('close', function () { +socket.on('close', function() { console.log('Connection closed.'); process.exit(1); }); diff --git a/examples/webchannel/qwclient/qwclient.js b/examples/webchannel/qwclient/qwclient.js index 0e28328..0b0909f 100755 --- a/examples/webchannel/qwclient/qwclient.js +++ b/examples/webchannel/qwclient/qwclient.js @@ -49,6 +49,7 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ + 'use strict'; var repl = require('repl'); var WebSocket = require('faye-websocket').Client; @@ -62,7 +63,7 @@ if (autoConnect === __filename) { autoConnect = false; } -var openChannel = function (address) { +var openChannel = function(address) { // this should be bound to the repl var self = this; address = address ? address : serverAddress; @@ -72,25 +73,27 @@ var openChannel = function (address) { var ws = new WebSocket(address); - ws.on('open', function (event) { + ws.on('open', function(event) { var transport = { - onmessage: function (data) {}, - send: function (data) { - ws.send(data, {binary: false}); - } + onmessage: function(data) {}, + send: function(data) { + ws.send(data, { + binary: false + }); + } }; - ws.on('message', function (event) { - transport.onmessage(event); + ws.on('message', function(event) { + transport.onmessage(event); }); // onmessage - var webChannel = new QWebChannel(transport, function (channel) { + var webChannel = new QWebChannel(transport, function(channel) { channels.push(channel); var channelIdx = (channels.length - 1); console.log('channel opened', channelIdx); // Create a nice alias to access this channels objects self.context['c' + channelIdx] = channel.objects; - ws.on('close', function () { + ws.on('close', function() { for (var i = 0; i < channels.length; ++i) { if (channels[i] === channel) { console.log('channel closed', i); @@ -102,7 +105,7 @@ var openChannel = function (address) { }); // new QWebChannel }); // onopen - ws.on('error', function (error) { + ws.on('error', function(error) { console.log('websocket error', error.message); }); }; // openChannel @@ -119,7 +122,7 @@ var setupRepl = function() { r.context.channels = channels; r.context.lsObjects = function() { - channels.forEach(function(channel){ + channels.forEach(function(channel) { console.log('Channel ' + channel); Object.keys(channel.objects); }); diff --git a/examples/webchannel/standalone/index.html b/examples/webchannel/standalone/index.html index a41bbbe..7c042cd 100644 --- a/examples/webchannel/standalone/index.html +++ b/examples/webchannel/standalone/index.html @@ -5,8 +5,7 @@