Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <meta http-equiv="X-UA-Compatible" content="ie=edge">
- <title>Document</title>
- </head>
- <body>
- <main>
- <header>
- <div class="user-count">0</div>
- <h1>Tokyo Chat</h1>
- </header>
- <section class="chat">
- </section>
- <form>
- <input type="text" placeholder="Say something nice" />
- <button>Send</button>
- </form>
- <style>
- /* ./public/style.css */
- * {
- box-sizing: border-box;
- transition: all .3s ease;
- }
- html, body {
- background: #eee;
- width: 100%;
- height: 100%;
- margin: 0;
- padding: 0;
- }
- main {
- width: calc(100% - 20px);
- max-width: 500px;
- margin: 0 auto;
- font-family: Helvetica, Arial, Sans, sans-serif;
- }
- h1, .user-count {
- margin: 0;
- padding: 10px 0;
- font-size: 32px;
- }
- .user-count {
- float: right;
- }
- .chat {
- content: '';
- width: 100%;
- height: calc(100vh - 165px);
- background: white;
- padding: 5px 10px;
- }
- .chat p {
- margin: 0 0 5px 0;
- }
- input, button {
- width: 100%;
- font: inherit;
- background: #fff;
- border: none;
- margin-top: 10px;
- padding: 5px 10px;
- }
- button:hover {
- cursor: pointer;
- background: #ddd;
- }
- @media all and (min-width: 500px) {
- .chat {
- height: calc(100vh - 140px);
- }
- input {
- width: calc(100% - 160px);
- }
- button {
- float: right;
- width: 150px;
- }
- }
- </style>
- <script>
- // /public/javascript.js
- // Get the current username from the cookies
- var user = cookie.get('user');
- if (!user) {
- // Ask for the username if there is none set already
- user = prompt('Choose a username:');
- if (!user) {
- alert('We cannot work with you like that!');
- } else {
- // Store it in the cookies for future use
- cookie.set('user', user);
- }
- }
- // Connect to the server-side websockets. But there's no server yet!
- var socket = io();
- // The user count. Can change when someone joins/leaves
- socket.on('count', function (data) {
- $('.user-count').html(data);
- });
- // When we receive a message
- // it will be like { user: 'username', message: 'text' }
- socket.on('message', function (data) {
- $('.chat').append('<p><strong>' + data.user + '</strong>: ' + data.message + '</p>');
- });
- // When the form is submitted
- $('form').submit(function (e) {
- // Avoid submitting it through HTTP
- e.preventDefault();
- // Retrieve the message from the user
- var message = $(e.target).find('input').val();
- // Send the message to the server
- socket.emit('message', {
- user: cookie.get('user') || 'Anonymous',
- message: message
- });
- // Clear the input and focus it for a new message
- e.target.reset();
- $(e.target).find('input').focus();
- });
- // ./public/javascript.js
- // Get the current username from the cookies
- var user = cookie.get('user');
- if (!user) {
- // Ask for the username if there is none set already
- user = prompt('Choose a username:');
- if (!user) {
- alert('We cannot work with you like that!');
- } else {
- // Store it in the cookies for future use
- cookie.set('user', user);
- }
- }
- var socket = io();
- // The user count. Can change when someone joins/leaves
- socket.on('count', function (data) {
- $('.user-count').html(data);
- });
- // When we receive a message
- // it will be like { user: 'username', message: 'text' }
- socket.on('message', function (data) {
- $('.chat').append('<p><strong>' + data.user + '</strong>: ' + data.message + '</p>');
- });
- // When the form is submitted
- $('form').submit(function (e) {
- // Avoid submitting it through HTTP
- e.preventDefault();
- // Retrieve the message from the user
- var message = $(e.target).find('input').val();
- // Send the message to the server
- socket.emit('message', {
- user: cookie.get('user') || 'Anonymous',
- message: message
- });
- // Clear the input and focus it for a new message
- e.target.reset();
- $(e.target).find('input').focus();
- });
- // /index.js
- const server = require('server');
- const { get, socket } = server.router;
- const { render } = server.reply;
- server([
- get('/', ctx => render('index.html'))
- ]);
- // /index.js
- const server = require('server');
- const { get, socket } = server.router;
- const { render } = server.reply;
- const updateCounter = ctx => {
- ctx.io.emit('count', ctx.io.sockets.sockets.length);
- };
- server([
- // For the initial load render the index.html
- get('/', ctx => render('index.html')),
- // Join/leave the room
- socket('connect', updateCounter),
- socket('disconnect', updateCounter)
- ]);
- // /index.js
- const server = require('server');
- const { get, socket } = server.router;
- const { render } = server.reply;
- // Update everyone with the current user count
- const updateCounter = ctx => {
- ctx.io.emit('count', Object.keys(ctx.io.sockets.sockets).length);
- };
- // Send the new message to everyone
- const sendMessage = ctx => {
- ctx.io.emit('message', ctx.data);
- };
- server([
- get('/', ctx => render('index.html')),
- socket('connect', updateCounter),
- socket('disconnect', updateCounter),
- socket('message', sendMessage)
- ]);
- </script>
- </main>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment