node.js websocket Server für WebRTC

websocket muss installiert sein!!!

npm install -g websocket

 

Hier mein Websocket Server js.
var WebSocketServer = require('websocket').server;
var http = require('http');
var clients = [];

var server = http.createServer(function(request, response) {
});
server.listen(1337, function() {
console.log((new Date()) + " Server is listening on port 1337");
});

wsServer = new WebSocketServer({
httpServer: server
});

function originIsAllowed(origin) {
console.log((new Date()) + ' originIsAllowed ' + origin + '.');
if (origin != "http://www.soho-piper.de"){
return false;
}
}
function sendCallback(err) {
if (err) console.error("send() error: " + err);
}
wsServer.on('request', function(request) {

if (!originIsAllowed(request.origin)){
request.reject();
console.log((new Date()) + ' Connection from origin ' + request.origin + ' rejected.');
return;
}

console.log((new Date()) + ' Connection from origin ' + request.origin + ' accepted.');
var connection = request.accept(null, request.origin);
console.log(' Connection ' + connection.remoteAddress);
clients.push(connection);

connection.on('message', function(message) {
if (message.type === 'utf8') {
console.log((new Date()) + ' Received Message ' + message.utf8Data);
clients.forEach(function (outputConnection) {
if (outputConnection != connection) {
outputConnection.send(message.utf8Data, sendCallback);
}
});
}
});

connection.on('close', function(connection) {
console.log((new Date()) + " Peer disconnected.");
});
});