-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
27,084 changed files
with
3,407,546 additions
and
0 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
**/node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const express = require('express'); | ||
const app = express(); | ||
const http = require('http'); | ||
const server = http.Server(app); | ||
const port = 7002; | ||
const io = require('socket.io')(server); | ||
const chatLib = require('./lib/chat.lib'); | ||
const newsLib = require('./lib/news.lib'); | ||
|
||
|
||
app.get('/chat', function(req, res){ | ||
res.sendFile(__dirname + '/public/chat.html'); | ||
}); | ||
|
||
app.get('/news', function(req, res){ | ||
res.sendFile(__dirname + '/public/news.html'); | ||
}); | ||
|
||
chatLib.socketFunc(io); | ||
newsLib.socketFunc(io); | ||
|
||
server.listen(port, () => { | ||
console.log(`Server started on port ${port}`) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const socketFunc = (io) => { | ||
const chat = io.of('/chat'); | ||
let onlineUsers = []; | ||
chat.on('connection', function (socket){ | ||
console.log('a user connected'); | ||
socket.on('user', function(user) { | ||
console.log('Connected user', user); | ||
onlineUsers.push(user); | ||
socket.user = user; | ||
socket.broadcast.emit('chat message', socket.user+" came online"); | ||
}); | ||
socket.on('chat message', function(msg) { | ||
chat.emit('chat message', msg); | ||
}); | ||
|
||
socket.on('disconnect', function() { | ||
console.log('User Disconnected'); | ||
if (socket.user) { | ||
let index = onlineUsers.indexOf(socket.user); | ||
onlineUsers.splice(index, 1); | ||
socket.broadcast.emit('chat message', socket.user+" left the chat"); | ||
} | ||
}); | ||
}); | ||
|
||
} | ||
|
||
module.exports = { | ||
socketFunc | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const socketFunc = (io) => { | ||
const news = io.of('/news'); | ||
|
||
news.on('connection', function (socket){ | ||
console.log('gajga a user connected'); | ||
|
||
socket.on('chat message', function(msg) { | ||
news.emit('chat message', msg); | ||
}); | ||
|
||
socket.on('disconnect', function() { | ||
console.log('User Disconnected'); | ||
}); | ||
}); | ||
|
||
} | ||
|
||
module.exports = { | ||
socketFunc | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<title>Socket.IO chat</title> | ||
<style> | ||
* { margin: 0; padding: 0; box-sizing: border-box; } | ||
body { font: 13px Helvetica, Arial; } | ||
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; } | ||
form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; } | ||
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; } | ||
#messages { list-style-type: none; margin: 0; padding: 0; } | ||
#messages li { padding: 5px 10px; } | ||
#messages li:nth-child(odd) { background: #eee; } | ||
</style> | ||
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> | ||
|
||
</head> | ||
<body> | ||
<ul id="messages"></ul> | ||
<form action=""> | ||
<input id="m" autocomplete="off" /><button>Send</button> | ||
</form> | ||
<script src="/socket.io/socket.io.js"></script> | ||
<script> | ||
$(function () { | ||
var socket = io('/chat'); // io('http://localhost', {path: '/nodejs/socket.io'}); | ||
$('form').submit(function(){ | ||
socket.emit('chat message', $('#m').val()); | ||
$('#m').val(''); | ||
return false; | ||
}); | ||
|
||
socket.on('chat message', function(msg){ | ||
$('#messages').append($('<li>').text(msg)); | ||
}); | ||
}); | ||
</script> | ||
</body> | ||
</html> |
Oops, something went wrong.