forked from jakecoffman/golang-websockets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.js
56 lines (47 loc) · 1.05 KB
/
chat.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
var app = angular.module("chat", []);
app.directive("chat", function($location, $anchorScroll){
return {
link: function(scope, element, attrs){
$location.hash('bottom');
scope.$watch("log", function(){
$anchorScroll();
}, true);
}
}
});
app.controller("MainCtl", function ($scope) {
$scope.log = [];
$scope.message = "";
var nick = prompt("Enter nickname:");
if (!window["WebSocket"]) {
$scope.log.push("Your browser does not support WebSockets.");
return;
}
var conn = new WebSocket("ws://localhost:8080/ws");
conn.onclose = function (e) {
$scope.$apply(function () {
$scope.log.push("Connection closed.");
})
};
conn.onmessage = function (e) {
$scope.$apply(function () {
$scope.log.push(e.data);
})
};
conn.onopen = function (e) {
console.log("Connected");
$scope.$apply(function () {
$scope.log.push("Welcome to the chat!");
})
};
$scope.send = function () {
if (!conn) {
return;
}
if (!$scope.message) {
return;
}
conn.send(nick + ": " + $scope.message);
$scope.message = "";
}
});