Skip to content

Commit

Permalink
Fix a couple of node incompatibilities in server
Browse files Browse the repository at this point in the history
Fixes node incompatibilities in the server on webrtc-from-chat, and
cleans some tab issues.
  • Loading branch information
Eric Shepherd committed May 19, 2016
1 parent 7700b83 commit 8795122
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 77 deletions.
2 changes: 1 addition & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"jquery": false,
"latedef": true,
"noarg": true,
"node": false,
"node": true,
"nonbsp": true,
"nonew": true,
"nonstandard": false,
Expand Down
152 changes: 76 additions & 76 deletions s/webrtc-from-chat/chatserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

"use strict";

var http = require('http');
//var http = require('http');
var https = require('https');
var url = require('url');
var fs = require('fs');
Expand Down Expand Up @@ -137,7 +137,7 @@ function sendUserListToAll() {
// Load the key and certificate data to be used for our HTTPS/WSS
// server.

const options = {
var httpsOptions = {
key: fs.readFileSync("/etc/pki/tls/private/mdn.key"),
cert: fs.readFileSync("/etc/pki/tls/certs/mdn.crt")
};
Expand Down Expand Up @@ -205,84 +205,84 @@ wsServer.on('request', function(request) {
// to the server.

connection.on('message', function(message) {
if (message.type === 'utf8') {
log("Received Message: " + message.utf8Data);

// Process incoming data.

var sendToClients = true;
msg = JSON.parse(message.utf8Data);
var connect = getConnectionForID(msg.id);

// Take a look at the incoming object and act on it based
// on its type. Unknown message types are passed through,
// since they may be used to implement client-side features.
// Messages with a "target" property are sent only to a user
// by that name.

switch(msg.type) {
// Public, textual message
case "message":
msg.name = connect.username;
msg.text = msg.text.replace(/(<([^>]+)>)/ig,"");
break;

// Username change
case "username":
var nameChanged = false;
var origName = msg.name;

// Ensure the name is unique by appending a number to it
// if it's not; keep trying that until it works.
while (!isUsernameUnique(msg.name)) {
msg.name = origName + appendToMakeUnique;
appendToMakeUnique++;
nameChanged = true;
}

// If the name had to be changed, we send a "rejectusername"
// message back to the user so they know their name has been
// altered by the server.
if (nameChanged) {
var changeMsg = {
id: msg.id,
type: "rejectusername",
name: msg.name
};
connect.sendUTF(JSON.stringify(changeMsg));
}

// Set this connection's final username and send out the
// updated user list to all users. Yeah, we're sending a full
// list instead of just updating. It's horribly inefficient
// but this is a demo. Don't do this in a real app.
connect.username = msg.name;
sendUserListToAll();
sendToClients = false; // We already sent the proper responses
break;
if (message.type === 'utf8') {
log("Received Message: " + message.utf8Data);

// Process incoming data.

var sendToClients = true;
msg = JSON.parse(message.utf8Data);
var connect = getConnectionForID(msg.id);

// Take a look at the incoming object and act on it based
// on its type. Unknown message types are passed through,
// since they may be used to implement client-side features.
// Messages with a "target" property are sent only to a user
// by that name.

switch(msg.type) {
// Public, textual message
case "message":
msg.name = connect.username;
msg.text = msg.text.replace(/(<([^>]+)>)/ig, "");
break;

// Username change
case "username":
var nameChanged = false;
var origName = msg.name;

// Ensure the name is unique by appending a number to it
// if it's not; keep trying that until it works.
while (!isUsernameUnique(msg.name)) {
msg.name = origName + appendToMakeUnique;
appendToMakeUnique++;
nameChanged = true;
}

// Convert the revised message back to JSON and send it out
// to the specified client or all clients, as appropriate. We
// pass through any messages not specifically handled
// in the select block above. This allows the clients to
// exchange signaling and other control objects unimpeded.

if (sendToClients) {
var msgString = JSON.stringify(msg);
var i;

// If the message specifies a target username, only send the
// message to them. Otherwise, send it to every user.
if (msg.target && msg.target !== undefined && msg.target.length !== 0) {
sendToOneUser(msg.target, msgString);
} else {
for (i=0; i<connectionArray.length; i++) {
connectionArray[i].sendUTF(msgString);
}
}
// If the name had to be changed, we send a "rejectusername"
// message back to the user so they know their name has been
// altered by the server.
if (nameChanged) {
var changeMsg = {
id: msg.id,
type: "rejectusername",
name: msg.name
};
connect.sendUTF(JSON.stringify(changeMsg));
}

// Set this connection's final username and send out the
// updated user list to all users. Yeah, we're sending a full
// list instead of just updating. It's horribly inefficient
// but this is a demo. Don't do this in a real app.
connect.username = msg.name;
sendUserListToAll();
sendToClients = false; // We already sent the proper responses
break;
}

// Convert the revised message back to JSON and send it out
// to the specified client or all clients, as appropriate. We
// pass through any messages not specifically handled
// in the select block above. This allows the clients to
// exchange signaling and other control objects unimpeded.

if (sendToClients) {
var msgString = JSON.stringify(msg);
var i;

// If the message specifies a target username, only send the
// message to them. Otherwise, send it to every user.
if (msg.target && msg.target !== undefined && msg.target.length !== 0) {
sendToOneUser(msg.target, msgString);
} else {
for (i=0; i<connectionArray.length; i++) {
connectionArray[i].sendUTF(msgString);
}
}
}
}
});

// Handle the WebSocket "close" event; this means a user has logged off
Expand Down

0 comments on commit 8795122

Please sign in to comment.