summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorKai Koehne <kai.koehne@qt.io>2017-08-28 16:31:05 +0200
committerKai Koehne <kai.koehne@qt.io>2017-09-08 15:31:30 +0000
commitdd04d74769e1859d571de7c4f2e88cb85a227ab9 (patch)
treeeac49cc7c3b3cb0b2f47c0d0bccf4d1888fb4649 /examples
parent13294ce605751babad0687e63c033436d129e3d2 (diff)
downloadqtwebchannel-dd04d74769e1859d571de7c4f2e88cb85a227ab9.tar.gz
Examples: beautify JavaScript snippets
Change-Id: I3499bfa49e79ed3664d020f7de0e691afeb15a86 Reviewed-by: Milian Wolff <milian.wolff@kdab.com>
Diffstat (limited to 'examples')
-rw-r--r--examples/webchannel/chatclient-html/chatclient.html24
-rw-r--r--examples/webchannel/chatclient-qml/qmlchatclient.qml58
-rw-r--r--examples/webchannel/nodejs/chatclient.js13
-rwxr-xr-xexamples/webchannel/qwclient/qwclient.js27
-rw-r--r--examples/webchannel/standalone/index.html12
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 + '<br>');
+ $('#chat').append("[" + time + "] " + user + ": " + message + '<br>');
});
//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 @@
</div>
<script>
$('#loginForm').submit(submitForm);
+
function submitForm(event) {
console.log("DEBUG login: " + channel);
channel.objects.chatserver.login($('#loginname').val(), function(arg) {
console.log("DEBUG login response: " + arg);
- if(arg === true) {
+ if (arg === true) {
$('#loginError').hide();
$('#loginDialog').dialog('close');
window.loggedin = true;
@@ -87,10 +85,9 @@
}
});
console.log($('#loginname').val());
- if(event !== undefined)
+ if (event !== undefined)
event.preventDefault();
return false;
-
}
</script>
</div>
@@ -110,10 +107,11 @@
</div>
<script>
$('#messageForm').submit(submitMessage);
+
function submitMessage(event) {
channel.objects.chatserver.sendMessage($('#loginname').val(), $('#message').val());
$('#message').val('');
- if(event !== undefined)
+ if (event !== undefined)
event.preventDefault();
return false;
}
@@ -122,7 +120,7 @@
<script type="text/javascript">
-$(document).ready(function(){
+$(document).ready(function() {
$('#loginError').hide();
});
</script>
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 @@
<script type="text/javascript" src="./qwebchannel.js"></script>
<script type="text/javascript">
//BEGIN SETUP
- function output(message)
- {
+ function output(message) {
var output = document.getElementById("output");
output.innerHTML = output.innerHTML + message + "\n";
}
@@ -19,16 +18,13 @@
output("Connecting to WebSocket server at " + baseUrl + ".");
var socket = new WebSocket(baseUrl);
- 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() {
output("WebSocket connected, setting up QWebChannel.");
new QWebChannel(socket, function(channel) {
// make core object accessible globally