geting multiple messages
See original GitHub issueHi,
I am using socket.io for chating. mycode: app.js:
var express = require(‘express’); var app = express(); var http = require( “http” ).createServer( app ); var io = require( “socket.io” )( http ); var port = process.env.PORT || ‘8088’; http.listen(port); app.set(‘views’, path.join(__dirname, ‘views’)); app.set(‘view engine’, ‘jade’); app.use(‘/’, routes); app.use(‘/users’, users); app.use(function(req,res,next){ req.io = io; next(); }); module.exports = app;
index.js:
var express = require(‘express’);
var router = express.Router();
router.get(‘/’, function(req, res, next) {
res.render(‘index’, { title: ‘Title’ });
});
router.get(‘/chat’, function(req, res) {
// usernames which are currently connected to the chat
var usernames = {};
var numUsers = 0;
var io=req.io;
io.on(‘connection’, function (socket) {console.log(“5”);
var addedUser = false;
// when the client emits 'new message', this listens and executes
socket.on('new message', function (data) {
// we tell the client to execute 'new message'
socket.broadcast.emit('new message', {
username: socket.username,
message: data
});
});
// when the client emits 'add user', this listens and executes
socket.on('add user', function (username) {
// we store the username in the socket session for this client
socket.username = username;
// add the client's username to the global list
//usernames[username] = username;
++numUsers;
addedUser = true;
socket.emit('login', {
numUsers: numUsers
});
// echo globally (all clients) that a person has connected
socket.broadcast.emit('user joined', {
username: socket.username,
numUsers: numUsers
});
});
// when the client emits 'typing', we broadcast it to others
socket.on('typing', function () {
socket.broadcast.emit('typing', {
username: socket.username
});
});
// when the client emits 'stop typing', we broadcast it to others
socket.on('stop typing', function () {
socket.broadcast.emit('stop typing', {
username: socket.username
});
});
// when the user disconnects.. perform this
socket.on('disconnect', function () {
// remove the username from global usernames list
if (addedUser) {
delete usernames[socket.username];
--numUsers;
// echo globally that this client has left
socket.broadcast.emit('user left', {
username: socket.username,
numUsers: numUsers
});
}
});
});
res.render('chat', { title: 'Chat' });
}); module.exports = router;
chat.jade:
doctype html
html(lang=“en”)
head
meta(charset=“UTF-8”)
link(rel=“stylesheet”, href=“/stylesheets/style.css”)
body
ul.pages
li.chat.page
.chatArea
ul.messages
input.inputMessage(type=“text”, placeholder=“Type here…”)
li.login.page
.form
h3.title What"s your nickname?
input.usernameInput(type=“text”, maxlength=“14”)
script(src=“https://code.jquery.com/jquery-1.10.2.min.js”)
script(src=“/javascripts/socket.io.js”)
script(src=“/javascripts/main.js”)
When run the above script working fine, but getting multiple messages Ex: if i refresh the page n times getting n messages
Help me.
Thanks Pokal
Issue Analytics
- State:
- Created 8 years ago
- Comments:20 (1 by maintainers)
Top GitHub Comments
use
socket.removeAllListeners()
at start up.Thanks man!