Answer the question
In order to leave comments, you need to log in
Creating a chat on node?
installed node on the hosting, opened port 3000, the port is listening, when lptf.it:3000 or 185.53.160.215:3000 is accessed , the Hello Node.js Server! message is displayed. Everything seemed to be fine! but! I want to create a chat further, and in the root of the site I create a file (server) with code (example from the Internet, I'm new):
var express = require('express');
var app = express();
app.set('port', process.env.PORT || 3000);
app.set('host', process.env.HOST || 'localhost');
var logger = require('log4js').getLogger();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var port = 3000;
app.use(express.static('/home/urlyogyj/public_html/views/chat.php'));
app.get('/chat.php', function(req,res){
res.sendFile('/home/urlyogyj/public_html/views/chat.php');
});
app.use('/fonts', express.static('/home/urlyogyj/node_modules/uikit/src/fonts/'));
app.get('/socket.io.js', function(req,res){
res.sendFile('/home/urlyogyj/node_modules/socket.io-client/dist/socket.io.js');
});
app.get('/jquery.js', function(req,res){
res.sendFile('/home/urlyogyj/node_modules/jquery/dist/jquery.min.js');
});
app.get('/uikit.js', function(req,res){
res.sendFile('/home/urlyogyj/node_modules/uikit/dist/js/uikit.min.js');
});
app.get('/uikit.css', function(req,res){
res.sendFile('/home/urlyogyj/node_modules/uikit/dist/css/uikit.almost-flat.min.css');
});
app.get('/animate.css', function(req,res){
res.sendFile('/home/urlyogyj/node_modules/animate.css/animate.min.css');
});
function usersCountToLog(){
logger.info('User count: '+io.engine.clientsCount);
}
io.on('connection', function(socket){
function setName(name){
if(name != undefined && name != ''){
socket.session = {};
socket.session.userName = name;
socket.session.address = socket.handshake.address;
socket.session.id = socket.id;
socket.broadcast.emit('newUser', socket.session);
socket.emit('userName', socket.session);
socket.emit('userList', io.length);
logger.info('User '+socket.session.userName+' join from IP: '+socket.session.address);
usersCountToLog();
var clients = io.sockets.connected;
var clientsList = {}
for(var key in clients){
if(clients[key].session)
clientsList[key] = clients[key].session;
}
socket.emit('clientsList', clientsList);
console.log(clientsList);
}
else
socket.emit('setName');
}
setName(null);
socket.on('setName', function(name){
if(name.length > 0)
setName(name);
else
socket.emit('setName');
});
socket.on('message', function(msg){
if(socket.session){
if(socket.session.userName === null || socket.session.userName == '' || socket.session.userName == undefined){
socket.emit('setName');
}else{
logger.trace('-----------');
logger.trace('User: ' + socket.session.userName + ' |
Message: ' + msg);
logger.trace('====> Sending message to other chaters...');
socket.broadcast.emit('messageFromClients', msg,
socket.session.userName);
socket.emit('messageToClients', msg,
socket.session.userName);
}
}
});
socket.on('disconnect', function(){
if(socket.session){
io.sockets.emit('userDisconnected', socket.session);
logger.info('User '+socket.session.userName+' left chat');
usersCountToLog();
}
});
});
console.log('server started, tipa...');
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question