Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed dependencies and simplified the app #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "vendor/github.com/gorilla/websocket"]
path = vendor/github.com/gorilla/websocket
url = [email protected]:gorilla/websocket.git
3 changes: 1 addition & 2 deletions src/main.go → main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ var upgrader = websocket.Upgrader{

// Define our message object
type Message struct {
Email string `json:"email"`
Username string `json:"username"`
Message string `json:"message"`
}

func main() {
// Create a simple file server
fs := http.FileServer(http.Dir("../public"))
fs := http.FileServer(http.Dir("./public"))
http.Handle("/", fs)

// Configure websocket route
Expand Down
26 changes: 7 additions & 19 deletions public/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ new Vue({
ws: null, // Our websocket
newMsg: '', // Holds new messages to be sent to the server
chatContent: '', // A running list of chat messages displayed on the screen
email: null, // Email address used for grabbing an avatar
username: null, // Our username
joined: false // True if email and username have been filled in
},
Expand All @@ -16,10 +15,9 @@ new Vue({
this.ws.addEventListener('message', function(e) {
var msg = JSON.parse(e.data);
self.chatContent += '<div class="chip">'
+ '<img src="' + self.gravatarURL(msg.email) + '">' // Avatar
+ '<img src="https://robohash.org/' + msg.username + '?size=32x32">' // Avatar
+ msg.username
+ '</div>'
+ emojione.toImage(msg.message) + '<br/>'; // Parse emojis
+ '</div>' + msg.message + '<br/>';

var element = document.getElementById('chat-messages');
element.scrollTop = element.scrollHeight; // Auto scroll to the bottom
Expand All @@ -28,34 +26,24 @@ new Vue({

methods: {
send: function () {
if (this.newMsg != '') {
if (this.newMsg) {
this.ws.send(
JSON.stringify({
email: this.email,
username: this.username,
message: $('<p>').html(this.newMsg).text() // Strip out html
message: this.newMsg.replace(/<(?:.|\n)*?>/gm, '') // Strip out html
}
));
this.newMsg = ''; // Reset newMsg
}
},

join: function () {
if (!this.email) {
Materialize.toast('You must enter an email', 2000);
return
}
if (!this.username) {
Materialize.toast('You must choose a username', 2000);
alert('You must choose a username', 2000);
return
}
this.email = $('<p>').html(this.email).text();
this.username = $('<p>').html(this.username).text();
this.username = this.username.replace(/<(?:.|\n)*?>/gm, '');
this.joined = true;
},

gravatarURL: function(email) {
return 'http://www.gravatar.com/avatar/' + CryptoJS.MD5(email);
}
}
});
});
11 changes: 1 addition & 10 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
<title>Simple Chat</title>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.8/css/materialize.min.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/emojione/2.2.6/assets/css/emojione.min.css"/>
<link rel="stylesheet" href="/style.css">

</head>
Expand Down Expand Up @@ -39,9 +37,6 @@
</div>
</div>
<div class="row" v-if="!joined">
<div class="input-field col s8">
<input type="email" v-model.trim="email" placeholder="Email">
</div>
<div class="input-field col s8">
<input type="text" v-model.trim="username" placeholder="Username">
</div>
Expand All @@ -56,10 +51,6 @@
<footer class="page-footer">
</footer>
<script src="https://unpkg.com/[email protected]/dist/vue.min.js"></script>
<script src="https://cdn.jsdelivr.net/emojione/2.2.6/lib/js/emojione.min.js"></script>
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/md5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.8/js/materialize.min.js"></script>
<script src="/app.js"></script>
</body>
</html>
</html>
1 change: 1 addition & 0 deletions vendor/github.com/gorilla/websocket
Submodule websocket added at 3ff332