code: change color scheme

master
ALI Hamza 2020-09-02 10:37:23 +07:00
parent 02f22219c6
commit ddab953b8d
Signed by: hamza
GPG Key ID: 22473A32291F8CB6
5 changed files with 162 additions and 134 deletions

@ -1,6 +1,7 @@
package main package main
import ( import (
"fmt"
"strings" "strings"
"time" "time"
@ -8,17 +9,23 @@ import (
) )
var usernames = make(map[*websocket.Conn]string) var usernames = make(map[*websocket.Conn]string)
var nameToConn = make(map[string]*websocket.Conn)
func handleIncomingMessage(sender *websocket.Conn, msg string) { func handleIncomingMessage(sender *websocket.Conn, msg string) {
if _, ok := usernames[sender]; !ok { if _, ok := usernames[sender]; !ok {
username := strings.TrimSpace(msg) username := strings.TrimSpace(msg)
if username == "" || username == "server" { if username == "" || username == "server" {
sender.WriteJSON(newError("You have an illegal nickname")) sender.WriteJSON(newError("You have an illegal username."))
return
}
if _, ok = nameToConn[username]; ok {
sender.WriteJSON(newError("The specified username is already taken."))
return return
} }
sendUserList(sender) sendUserList(sender)
usernames[sender] = username usernames[sender] = username
nameToConn[username] = sender
m := newMessage(msgJoin, "server", username) m := newMessage(msgJoin, "server", username)
m.dispatch() m.dispatch()
return return
@ -28,9 +35,16 @@ func handleIncomingMessage(sender *websocket.Conn, msg string) {
} }
func handleDisconnection(sender *websocket.Conn) { func handleDisconnection(sender *websocket.Conn) {
m := newMessage(msgLeave, "server", usernames[sender]) username, ok := usernames[sender]
if !ok {
fmt.Println("leave without registering")
return
}
m := newMessage(msgLeave, "server", username)
m.dispatch() m.dispatch()
delete(usernames, sender) delete(usernames, sender)
delete(nameToConn, username)
} }
type messageType string type messageType string

@ -3,6 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"strconv"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
) )
@ -33,12 +34,15 @@ func websocketConnection(w http.ResponseWriter, r *http.Request) {
} }
} }
const port = 8080
func main() { func main() {
http.HandleFunc("/websocket", websocketConnection) http.HandleFunc("/websocket", websocketConnection)
http.Handle("/", http.FileServer(http.Dir("./static"))) http.Handle("/", http.FileServer(http.Dir("./static")))
err := http.ListenAndServe(":8080", nil) fmt.Println("Chat server opening on port", port)
err := http.ListenAndServe(":"+strconv.Itoa(port), nil)
if err != nil { if err != nil {
fmt.Errorf("Server could not start up: %s", err.Error()) _ = fmt.Errorf("Server could not start up: %s", err.Error())
} }
} }

@ -1,15 +1,11 @@
@import url('https://fonts.googleapis.com/css?family=Open+Sans:400,700&display=swap'); @import url('https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,400;0,700;0,900;1,400&display=swap');
body { body {
justify-content: center;
align-items: center;
background-color: #19180A;
width: 100vw; width: 100vw;
height: 100vh; height: 100vh;
padding: 0; padding: 0;
margin: 0; margin: 0;
font-family: 'Open Sans', sans-serif; font-family: 'Roboto', sans-serif;
display: flex;
} }
#snackbar { #snackbar {
@ -35,136 +31,92 @@ body {
} }
.container { .container {
width: 100%;
height: 100%;
display: flex; display: flex;
flex-wrap: wrap; flex-wrap: wrap;
justify-content: space-between; justify-content: space-between;
margin: 32px;
width: 90%;
height: 90%;
}
.messages {
width: 82%;
height: 80%;
overflow-y: scroll;
border-radius: 4px;
background-color: #4D5359;
}
.messages > div {
padding: 20px;
} }
.message hr { .lhs {
border: none; width: calc(100% - 300px);
border-bottom: 1px solid transparent; display: flex;
border-bottom-color: #6c6882; flex-direction: column-reverse;
margin: 14px 0; background-color: #39363F;
} }
.message .new{ .rhs {
border-bottom-color: #BA5C5C; width: calc(300px - 24px);
background-color: #28272D;
padding: 12px;
color: #eeeeee;
} }
.message .author { .messages {
line-height: 24px; overflow: auto;
line-height: 24px; max-height: calc(100vh - 128px);
color: #AAB3FF; flex-grow: 1;
display: inline;
font-weight: bold;
} }
.message .timestamp { .messages::-webkit-scrollbar {
line-height: 24px; width: 8px;
color: #888888;
display: inline;
font-size: 12px;
} }
.message .content { .messages::-webkit-scrollbar-track {
color: #C9CEB1; background: #322F37;
border-radius: 12px;
} }
.message .server-message { .messages::-webkit-scrollbar-thumb {
padding: 4px 0 4px 20px; background: #504D56;
margin: 6px 0; border-radius: 12px;
font-weight: bold;
color: #AAB3FF;
border-left: 4px solid #EAF0CE;
background-color: #EAF0CE22;
} }
.users { .messages::-webkit-scrollbar-thumb:hover {
width: 15%; background: #5C5766;
height: 80%;
border-radius: 4px;
background-color: #4D5359;
background: -moz-linear-gradient(left, #4d5359 0%, #324a59 100%);
background: -webkit-linear-gradient(left, #4d5359 0%,#324a59 100%);
background: linear-gradient(to bottom, #4d5359 0%,#324a59 100%);
font-weight: 200px;
text-align: center;
overflow-y: scroll;
} }
.users h3 {
color: #EAF0CE;
font-weight: 100px;
}
.users p { /* */
text-align: left; .chat {
color: #AAB3FF; height: 64px;
margin: 16px 6%; padding: 32px;
padding: 3px 9%;
border-left: 4px solid #EAF0CE;
background-color: #EAF0CE22;
text-overflow: ellipsis;
overflow-x: hidden;
white-space: nowrap;
} }
.chat { .chat-inner {
height:64px;
display: flex; display: flex;
align-items: center; align-items: center;
width: 96%;
height: 8%;
padding: 9px;
border-radius: 8px;
background-color: #4d5359;
} }
.primary { #chatI {
height: 64px;
width: 100%; width: 100%;
background: -moz-linear-gradient(left, #4d5359 0%, #324a59 100%); padding: 0 12px;
background: -webkit-linear-gradient(left, #4d5359 0%,#324a59 100%); font-size: 20px;
background: linear-gradient(to bottom, #4d5359 0%,#324a59 100%); color: #eeeeee;
padding: 12px 32px 12px; border: none;
border-radius: 6px 0 0 6px;
background: #28272D;
} }
input { #chatI:focus {
font-family: Helvetica;
font-weight: 300;
width: 100%;
line-height: 2.2rem;
font-size: 1.1rem;
background-color: transparent;
border: none;
outline: none; outline: none;
color: #AAB3FF;
padding: 0;
margin: 0;
} }
input::placeholder{ #chatI::placeholder {
color: #999; color: #908D9B;
} }
button { #chatB {
height: 64px;
padding: 0; padding: 0;
background: none; width: 128px;
font-size: 24px;
color: #000;
border: none; border: none;
outline: none; border-radiuS: 0 6px 6px 0;
background: #8EFFD6;
} }
i { i {
@ -177,6 +129,40 @@ i:hover {
color: #EAF0CE; color: #EAF0CE;
} }
/* Chat Messages */
.message {
margin: 32px 32px;
}
.server-message {
color: #908D9B;
font-style: italic;
font-size: 21px;
}
.author {
color: #fcfcfc;
font-weight: 700;
display: inline-block;
margin-right: 8px;
}
.author.self {
color: #8EFFD6;
}
.timestamp {
display: inline-block;
font-weight: 400;
font-size: 14px;
color: #7D7A87;
}
.content {
color: #eee;
margin-top: 2px;
}
.modal { .modal {
display: none; display: none;
position: fixed; position: fixed;
@ -192,15 +178,14 @@ i:hover {
} }
.modal-content { .modal-content {
color: #AAB3FF; color: #eeeeee;
position: relative; position: relative;
background-color: #324a59; background-color: #39363F;
margin: auto; margin: auto;
padding: 18px; padding: 18px;
padding-bottom: 36px; padding-bottom: 36px;
border-radius: 4px; border-radius: 4px;
width: 60%; width: 60%;
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
-webkit-animation-name: animatetop; -webkit-animation-name: animatetop;
-webkit-animation-duration: 0.6s; -webkit-animation-duration: 0.6s;
} }
@ -209,6 +194,34 @@ i:hover {
padding: 18px 0; padding: 18px 0;
} }
#nickI {
height: 48px;
padding: 0 12px;
font-size: 16px;
color: #eeeeee;
border: none;
border-radius: 6px;
background: #28272D;
}
#nickI:focus {
outline: none;
}
#nickI::placeholder {
color: #908D9B;
}
#nickB {
height: 48px;
padding: 0;
width: 96px;
font-size: 18px;
color: #000;
border: none;
border-radiuS: 6px;
background: #8EFFD6;
}
@-webkit-keyframes animatetop { @-webkit-keyframes animatetop {
from {top:-300px; opacity:0} from {top:-300px; opacity:0}
to {top:0; opacity:1} to {top:0; opacity:1}

@ -6,31 +6,29 @@
<link rel="stylesheet" type="text/css" href="/css/style.css"> <link rel="stylesheet" type="text/css" href="/css/style.css">
<link rel="stylesheet" type="text/css" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css"> <link rel="stylesheet" type="text/css" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
</head> </head>
<div id="snackbar">Warning: This is some message.</div> <div id="snackbar"></div>
<body> <body>
<div id="modal" class="modal"> <div id="modal" class="modal">
<div class="modal-content"> <div class="modal-content">
<h1>Enter a username:</h1> <h1>Enter a username:</h1>
<div class="chat"> <input type="text" id="nickI" placeholder="Nickname">
<input type="text" id="nickI" placeholder="Nickname"> <button type="submit" id="nickB">Join </button>
<button type="submit" id="nickB"><i class="icon ion-android-arrow-forward"></i></button>
</input>
</div>
</div> </div>
</div> </div>
<div class="container"> <div class="container">
<div class="messages"> <div class="lhs">
<div id="messages"> <div class="chat">
<div class="chat-inner">
<input type="text" id="chatI" placeholder="Enter your message">
<button id="chatB">Send </button>
</div>
</div>
<div class="messages" id="messages">
</div> </div>
</div> </div>
<div class="users" id="users"> <div class="rhs" id="users">
<h3>Online Users</h3> <h3>Online Users</h3>
</div> </div>
<div class="chat primary">
<input type="text" id="chatI" placeholder="Enter your message">
<button type="submit" id="chatB"><i class="icon ion-android-arrow-forward"></i></button>
</input>
</div>
</div> </div>
<script src="/js/index.js"></script> <script src="/js/index.js"></script>
</body> </body>

@ -5,10 +5,6 @@ let nickname = window.localStorage.getItem("nickname");
let nickI = document.getElementById("nickI"); let nickI = document.getElementById("nickI");
let nickB = document.getElementById("nickB"); let nickB = document.getElementById("nickB");
if (nickname != null) {
nickI.value = nickname;
}
//Chatting button and input //Chatting button and input
let chatI = document.getElementById("chatI"); let chatI = document.getElementById("chatI");
let chatB = document.getElementById("chatB"); let chatB = document.getElementById("chatB");
@ -20,6 +16,10 @@ let modal = document.getElementById("modal");
let protocol = location.protocol == "http:" ? "ws:" : "wss:"; let protocol = location.protocol == "http:" ? "ws:" : "wss:";
let url = protocol + "//" + location.host + "/websocket"; let url = protocol + "//" + location.host + "/websocket";
if (nickname != null) {
nickI.value = nickname;
}
let ws = new WebSocket(url); let ws = new WebSocket(url);
ws.onopen = function() { ws.onopen = function() {
@ -68,9 +68,12 @@ function newMessage(data) {
function chatMessage(sender, content, date) { function chatMessage(sender, content, date) {
let msg = create("div", "message"); let msg = create("div", "message");
let hr = create("hr", "seperator");
let author = create("div", "author"); aClasses = "author"
if (sender === nickname) {
aClasses += " self"
}
let author = create("div", aClasses);
author.innerText = sender; author.innerText = sender;
let timestamp = create("div", "timestamp"); let timestamp = create("div", "timestamp");
@ -79,21 +82,17 @@ function chatMessage(sender, content, date) {
let text = create("div", "content"); let text = create("div", "content");
text.innerText = content; text.innerText = content;
msg.append(hr, author, timestamp, text); msg.append(author, timestamp, text);
insertMessage(msg); insertMessage(msg);
} }
function serverMessage(content, date) { function serverMessage(content, date) {
let msg = create("div", "message"); let msg = create("div", "message");
let hr = create("hr", "seperator");
let timestamp = create("div", "timestamp");
timestamp.innerText = dateToString(new Date(date));
let text = create("div", "server-message"); let text = create("div", "server-message");
text.innerText = content; text.innerText = content;
msg.append(hr, timestamp, text); msg.append(text);
insertMessage(msg); insertMessage(msg);
} }