I am trying to build a simple video chat service using webrtc. I am referring to the tutorial given in html5rocks article introducing webrtc basics..[http://www.html5rocks.com/en/tutorials/webrtc/basics/][1]
The server.js is working fine with node-static on the localhost with perfect video streaming and chat service.The problem is when i am trying to host it on heroku ..the video stream doesnt work or should i say none of the functionality of server.js work..Browser window just shows a text box which is the basic layout of my index.html file..So there is definitely some problem in my code .something is missing in server.js file which is required for hosting on heroku..Here is the code for Server.js..Please help me to resolve the issue..Tell me the correct way of hosting it as i know i am missing something here..
var static = require('node-static');
/*var http = require('http');
var file = new(static.Server)();
var app = http.createServer(function (req, res) {
file.serve(req, res);
}).listen(2013);
*/
var express = require('express');
var app = express();
console.log(express.static(__dirname + '/js'));
app.use(express.static(__dirname + '/js'));
app.all('*', function(req, res){
res.sendfile("index.html");
});
app.listen(9000);
var io = require('socket.io').listen(app);
io.sockets.on('connection', function (socket){
function log(){
var array = [">>> "];
for (var i = 0; i < arguments.length; i++) {
array.push(arguments[i]);
}
socket.emit('log', array);
}
socket.on('message', function (message) {
log('Got message: ', message);
socket.broadcast.emit('message', message); // should be room only
});
socket.on('create or join', function (room) {
var numClients = io.sockets.clients(room).length;
log('Room ' + room + ' has ' + numClients + ' client(s)');
log('Request to create or join room', room);
if (numClients == 0){
socket.join(room);
socket.emit('created', room);
} else if (numClients == 1) {
io.sockets.in(room).emit('join', room);
socket.join(room);
socket.emit('joined', room);
} else { // max two clients
socket.emit('full', room);
}
socket.emit('emit(): client ' + socket.id + ' joined room ' + room);
socket.broadcast.emit('broadcast(): client ' + socket.id + ' joined room ' + room);
});
});
Related
guys, I have this doubt with websockets in javascript, I want to return the ip's connected to the server and i don't have idea to do it, I only want the ip's, I just want the ones that are connected at the moment.
Here is my server:
var WebSocketServer = require('websocket').server;
var http = require('http');
var server = http.createServer(function(request, response) {
console.log((new Date()) + ' Received request for ' + request.url);
response.writeHead(404);
response.end();
});
server.listen(8080, function() {
console.log('Server is listening on port 8080');
});
wsServer = new WebSocketServer({
httpServer: server,
autoAcceptConnections: false
});
function originIsAllowed(origin) {
// put logic here to detect whether the specified origin is allowed.
return true;
}
wsServer.on('request', function(request) {
if (!originIsAllowed(request.origin)) {
// Make sure we only accept requests from an allowed origin
request.reject();
console.log((new Date()) + ' Connection from origin ' request.origin + ' rejected.');
return;
}
var connection = request.accept('echo-protocol', request.origin);
console.log((new Date()) + ' Connection accepted.');
connection.on('message', function(message) {
if (message.type === 'utf8') {
console.log('Received Message: ' + message.utf8Data);
var ipAddress = connection.remoteAddress;
console.log(ipAddress);
connection.sendUTF("pablo puta");
}
else if (message.type === 'binary') {
console.log('Received Binary Message of ' + message.binaryData.length + ' bytes');
connection.sendBytes(message.binaryData);
}
});
connection.on('close', function(reasonCode, description) {
console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
});
});
Please, if you can help me, I would be very grateful.
I believe you can keep an object of ips:
var ips={};
connection.on('close', function(reasonCode, description) {
delete ips[connection.remoteAddress];
});
connection.on('open', function(reasonCode, description) {
ips[connection.remoteAddress]=true;
});
And then somehow response with:
Object.keys(ips);
to yourself...
(I believe it must be this instead of connection inside the handlers, but im not an expert in that...)
I am using node.js, express, and sockets.io to:
Find out whether or not the user has included a specific URL parameter when they arrive (working)
If not, make a new random room ID and join the user to that room (working)
Modify the address bar of the browser to reflect the new URL without redirecting the browser (unsure if possible, or how to accomplish)
First, I do not know if this is possible without violating DOM security. If it is, I would like to understand how I might be able to do it.
If it is not possible, and/or if I need to use a redirect, could you show me how to do it within my already working code within the working context of items #1 and #2?
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var url = require('url');
app.use(express.static(__dirname + '/node_modules'));
app.get('/', function(req, res, next) {
res.sendFile(__dirname + '/index.html');
var roomID = req.query.me;
if (roomID == null || !io.sockets.adapter.rooms[roomID]) {
console.log("URL room variable is: " + roomID);
console.log('New room!');
roomID = Math.random().toString(36).substring(2, 13);
joinRoom(roomID);
console.log("+ you just joined new room: " + roomID);
} else {
joinRoom(roomID);
console.log("URL room variable is: " + roomID);
console.log("= you just joined existing room: " + roomID);
}
});
function joinRoom(roomID) {
io.on('connection', function(socket) {
socket.join(roomID);
io.sockets.in(roomID).emit('connectToRoom', "Joined room: http://localhost:3030/?me=" + roomID);
});
return;
}
server.listen(3030);
I do not have any sample code for the part that I'm trying to accomplish because I simply don't know where I should begin. So I would appreciate any direction (redirection? pun intended).
Thank you!
My http server on node.js using socket-io isn't running. I used npm install socket-io and running server.
This is the server's code.
var static = require('node-static');
var http = require('http');
// Create a node-static server instance
var file = new(static.Server)();
// We use the http moduleĆs createServer function and
// rely on our instance of node-static to serve the files
var app = http.createServer(function (req, res) {
file.serve(req, res);
}).listen(8181);
// Use socket.io JavaScript library for real-time web applications
var io = require('socket.io').listen(app);
// Let's start managing connections...
io.sockets.on('connection', function (socket){
// Handle 'message' messages
socket.on('message', function (message) {
log('S --> got message: ', message);
// channel-only broadcast...
socket.broadcast.emit('message', message);
});
// Handle 'create or join' messages
socket.on('create or join', function (room) {
/*var namespace = '/';
for (var numClients in io.nsps[namespace].adapter.rooms[room]) {
console.log(numClients);
}*/
//var numClients = io.sockets.clients(room).length;
var numClients = io.sockets.adapter.rooms[room]!=undefined ? Object.keys(io.sockets.adapter.rooms[room]).length:0;
log('SERVER: CLIENTS ' + room + ' has ' + numClients + ' client(s)');
log('S --> Room ' + room + ' has ' + numClients + ' client(s)');
log('S --> Request to create or join room', room);
// First client joining...
if (numClients == 0){
socket.join(room);
socket.emit('created', room);
} else if (numClients == 1) {
// Second client joining...
io.sockets.in(room).emit('join', room);
socket.join(room);
socket.emit('joined', room);
log('S: sent JOIN message');
} else { // max two clients
socket.emit('full', room);
}
});
function log(){
var array = [">>> "];
for (var i = 0; i < arguments.length; i++) {
array.push(arguments[i]);
}
socket.emit('log', array);
}
});
While this is client's code connecting to the socket:
var room = getParameterByName('room_name');
alert ("room_name: "+room);
// Connect to signalling server
var socket = io.connect();
// Send 'Create or join' message to singnalling server
if (room !== '') {
console.log('Create or join room', room);
socket.emit('create or join', room);
}
With netstat -a comand I saw what the server's port is active.. but it doesn't send messages.
I've another question: how can server sends messages not in broadcast, but to specified peers connected? I mean: instead of
socket.on('message', function (message) {
log('S --> got message: ', message);
// channel-only broadcast...
socket.broadcast.emit('message', message);
});
How could I do? Thanks!
I've another question: how can server sends messages not in broadcast,
but to specified peers connected?
The second part of your question is that you just do socket.emit() to send to a single socket like this to send back to the same socket that just sent you a message:
socket.on('message', function (message) {
log('S --> got message: ', message);
// channel-only broadcast...
socket.emit('message', "got your message");
});
Or, if you have a socket id value for some other connected client, you can fetch that socket on the server like this:
io.sockets.connected[id].emit(...)
I am trying to have two different Node processes (using Cluster) try to become servers to a Port. However, whenever the second process gets to the port, it doesn't detect that the port is being used.
I suspect the reason why they are not detecting if the port is open or not is due to the nature of callbacks (I'm detecting if the port is used or not using the portInUse function, so it is fetched asynchronously, and might cause some type of conflict later on).
Here is the code:
var cluster = require('cluster');
var net = require('net');
var PORT = 1337;
var list = {};
var portIsBeingUsed = false;
// Variable that detects if the port is in use.
var portInUse = function(port, callback) {
var server = net.createServer(function(socket) {
socket.write('Echo server\r\n');
socket.pipe(socket);
});
server.listen(port, 'localhost');
server.on('error', function (e) {
callback(true);
});
server.on('listening', function (e) {
server.close();
callback(false);
});
};
if (cluster.isMaster) {
for (var i = 0; i < 2; i++) {
cluster.fork();
}
Object.keys(cluster.workers).forEach(function(id) {
console.log("I am running with ID : "+ cluster.workers[id].process.pid);
list[cluster.workers[id].process.pid] = 0;
});
cluster.on('exit', function(worker, code, signal) {
console.log('worker ' + worker.process.pid + ' died');
});
} else { // Rest of the logic with all Processes goes here.
// Get the Process ID of the current process in execution.
var pid = cluster.worker.process.pid;
console.log("This is process " + pid + " working now.\n");
// Verify if Port is being used.
portInUse(PORT, function(returnValue) {
if(returnValue) { // Become a Client to the Server
console.log("port " + PORT + " is being used.\n\n");
becomeClient(pid);
} else { // Become a Server
console.log("port" + PORT + " is not being used.\n\n");
becomeServer(pid);
}
});
}
function becomeServer(pid) {
var server = list[pid];
server = net.createServer(function (socket) {
socket.write('Hello Server 1\r\n');
socket.end("hello");
console.log("Someone connected to Server 1. \n");
socket.pipe(socket);
});
server.listen(PORT, function(){
console.log("Process " + pid + " has become the Server on Port " + PORT);
});
server.on("error", function() {
console.log("there was an error on Process " + pid);
console.log("this error was becoming a Server.");
});
}
function becomeClient(pid) {
var client = list[pid];
client = net.connect({port: PORT}, function() {
list[pid].write("I am connected to the port and my pid is " + pid);
});
client.on('data', function(data) {
console.log(data.toString());
list[pid].end();
});
client.on('end', function() {
console.log('disconnected from server');
});
}
And here is the output:
So the first process (In this case Process 9120) becomes the server on port 1337, but then the second process doesn't detect that the port is being used and somehow becomes the server as well (I would expect an EADDRINUSE here, not sure why it isn't showing any errors).
Any help or clarification as to why this isn't working would be greatly appreciated.
Thanks,
This is expected behavior and it is how the cluster module works (by default). It's what allows incoming requests to the same port to be easily distributed among the available workers.
A large part of the point of cluster involves port sharing, so that you can have multiple workers take turns serving requests. The worker processes just request a port from the master, which actually opens the port, and then the master hands requests back to any workers that have requested that port.
I've got a Node.js server with websocket module, installed through the following command:
npm install websocket
Starting from this guide, I decided to extend it sharing the sent messages between all the clients.
Here is my (simplified) server code:
#!/usr/bin/env node
var WebSocketServer = require('websocket').server;
var http = require('http');
var server = http.createServer(function(request, response) {
console.log((new Date()) + ' Received request for ' + request.url);
response.writeHead(404);
response.end();
});
server.listen(8080, function() {
console.log((new Date()) + ' Server is listening on port 8080');
});
wsServer = new WebSocketServer({
httpServer: server,
autoAcceptConnections: false
});
var connectedClientsCount = 0; // ADDED
var connectedClients = []; // ADDED
wsServer.on('request', function(request) {
var connection = request.accept('echo-protocol', request.origin);
connectedClientsCount++;
connectedClients.push(connection);
console.log((new Date()) + ' Connection accepted.');
connection.on('message', function(message) {
if (message.type === 'utf8') {
console.log('Received Message: ' + message.utf8Data);
for(c in connectedClients) // ADDED
c.sendUTF(message.utf8Data); // ADDED
}
else if (message.type === 'binary') {
console.log('Received Binary Message of ' + message.binaryData.length + ' bytes');
connection.sendBytes(message.binaryData);
}
});
connection.on('close', function(reasonCode, description) {
// here I should delete the client...
console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
});
});
In this case I can get the connectedClientsCount value, but I can't manage the connectedClients list.
I also tried with ((eval)c).sendUTF(message.utf8Data); as for statement but it doesn't work.
I advise you to use Socket.IO: the cross-browser WebSocket for realtime apps. The module is very simple to install and configure
For example:
Server
...
io.sockets.on('connection', function (socket) {
//Sends the message or event to every connected user in the current namespace, except to your self.
socket.broadcast.emit('Hi, a new user connected');
//Sends the message or event to every connected user in the current namespace
io.sockets.emit('Hi all');
//Sends the message to one user
socket.emit('news', {data:'data'});
});
});
...
more
Client:
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost');
//receive message
socket.on('news', function (data) {
console.log(data);
//send message
socket.emit('my other event', { my: 'data' });
});
</script>
more about exposed events
Try replacing for ... in by for ... of
for(c of connectedClients) // ADDED
c.sendUTF(message.utf8Data); // ADDED