generated from github/welcome-to-github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_chat.html
152 lines (124 loc) · 4.01 KB
/
index_chat.html
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<!-- index_chat.html -->
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>Chatapp</title>
<style>
.name{
font-size: 10px;
opacity: 0.4;
}
.msg_main{
display: flex;
padding: 0 0 0 5px;
justify-content: space-between;
align-items: center;
}
.msg_left{
display: flex;
align-items: center;
}
.msg_right{
display: flex;
margin: 0 15px 0 0;
}
.icon{
border-radius: 50%;
}
.msg{
padding: 0 5px 0 5px;
}
.time{
font-size: 10px;
margin: 10px 0 0 0;
}
.text{
font-size: 16px;
font-weight: lighter;
border-radius: 10px;
padding: 5px 5px 5px 5px;
}
#output{
width: 300px;
}
</style>
</head>
<body>
<!-- 入力エリア -->
<div>
<div>
name<input type="text" id="username">
</div>
<div>
text<textarea id="text" rows="5"></textarea>
<button id="send">send</button>
</div>
<!-- 入力エリア終わり -->
<!-- 表示エリア -->
<div id="output"></div>
</div>
<!-- 表示エリア終わり -->
<script src="https://www.gstatic.com/firebasejs/5.3.1/firebase.js"></script>
<script>
// Your web app's Firebase configuration
var firebaseConfig = {
apiKey: "AIzaSyCwdcmrI7Qk_jyqL33c5V9gjQmoE4pgamE",
authDomain: "chatapp-fe2e4.firebaseapp.com",
databaseURL: "https://chatapp-fe2e4.firebaseio.com",
projectId: "chatapp-fe2e4",
storageBucket: "chatapp-fe2e4.appspot.com",
messagingSenderId: "684487472205",
appId: "1:684487472205:web:7eeac5ddc64ca3d6425804",
measurementId: "G-DY615TECHR"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
//Firebaseへの書き込み読み込み処理
//準備
const newPostRef = firebase.database();
let room = "room1";
const send = document.getElementById("send");
const username = document.getElementById("username");
const text = document.getElementById("text");
const output = document.getElementById("output");
//メッセージ送信処理
send.addEventListener('click',function(){
//書き込み処理
newPostRef.ref(room).push({
username: username.value,
text: text.value,
time: time()
});
text.value = "";
});
//メッセージ受信処理
newPostRef.ref(room).on("child_added",function(data){
const v = data.val();//データ取得
const k = data.key;//ユニークkey取得
let str = "";
str += '<div id="'+ k +'" class="msg_main">';
str += '<div class="msg_left">';
str += '<div class=""><img src="img/icon_person.png" alt"" class="icon '+ v.username+'" width="30"></div>';
str += '<div class="msg">';
str += '<div class="name">'+ v.username +'</div>';
str += '<div class="text">'+ v.text +'</div>';
str += '</div></div>';
str += '<div class="msg_right">';
str += '<div class="time">'+ v.time +'</div>';
str += '</div></div>';
output.innerHTML += str;
});
//時間を取得する
function time() {
var date = new Date();
var hh = ("0"+date.getHours()).slice(-2);
var min = ("0"+date.getMinutes()).slice(-2);
var sec = ("0"+date.getSeconds()).slice(-2);
var time = hh + ":" + min + ":" + sec;
return time;
}
</script>
<!--npm install -g firebase-tools-->
</body>
<html>