usestudio.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <title>Page Title</title>
  7. <meta name="viewport" content="width=device-width, initial-scale=1">
  8. <style lang="">
  9. body {
  10. margin: 0;
  11. padding: 0;
  12. }
  13. :focus {
  14. outline: 0
  15. }
  16. ::-webkit-scrollbar-track {
  17. background-color: transparent;
  18. }
  19. ::-webkit-scrollbar {
  20. width: 5px;
  21. background-color: transparent;
  22. }
  23. ::-webkit-scrollbar-thumb {
  24. background-color: #b8b8b8;
  25. border-radius: 15px;
  26. }
  27. .chat_room {
  28. width: 700px;
  29. height: 595px;
  30. border: 1px solid #cacaca;
  31. /* border-radius: 4px; */
  32. margin: auto;
  33. margin-top: 50px;
  34. position: relative;
  35. }
  36. .chat_room>.list {
  37. visibility: hidden;
  38. width: 160px;
  39. height: 100%;
  40. float: left;
  41. border-right: 1px solid #e7e7e7;
  42. }
  43. .chat_room .list .list_1 {
  44. box-sizing: border-box;
  45. padding: 8px;
  46. display: inline-block;
  47. width: 100%;
  48. position: relative;
  49. }
  50. .chat_room .list .on {
  51. background: #e8e8e8;
  52. }
  53. .chat_room .list .list_1:hover {
  54. background: #e8e8e8;
  55. }
  56. .chat_room .list .img {
  57. width: 38px;
  58. background: #ddd;
  59. float: left;
  60. height: 38px;
  61. border-radius: 55px;
  62. overflow: hidden;
  63. cursor: pointer;
  64. }
  65. .chat_room .list .img img {
  66. width: 100%;
  67. height: 100%;
  68. }
  69. .chat_room .list .title {
  70. font-size: 14px;
  71. text-indent: 9px;
  72. color: #353535;
  73. }
  74. .chat_room .list .info {
  75. font-size: 12px;
  76. color: #8f8f8f;
  77. text-indent: 13px;
  78. padding-top: 2px;
  79. }
  80. .chat_room>.content {
  81. float: right;
  82. width: calc(100% - 161px);
  83. height: 75%;
  84. border-bottom: 1px solid #e6e6e6;
  85. overflow: auto;
  86. position: absolute;
  87. right: 0;
  88. background: #fff;
  89. }
  90. .chat_room .input {
  91. float: left;
  92. width: calc(100% - 161px);
  93. height: 25%;
  94. position: absolute;
  95. bottom: 0;
  96. right: 0;
  97. }
  98. .chat_room .input .input_content {
  99. width: 100%;
  100. height: 66%;
  101. padding: 8px;
  102. box-sizing: border-box;
  103. overflow: overlay;
  104. margin-top: 4px;
  105. border: 0;
  106. display: block;
  107. resize: none;
  108. }
  109. .chat_room .input input {
  110. position: absolute;
  111. bottom: 15px;
  112. right: 15px;
  113. border: 0;
  114. border-radius: 2px;
  115. padding: 4px 11px;
  116. color: #fff;
  117. cursor: pointer;
  118. background: rgb(38, 131, 245);
  119. }
  120. .chat_room .content .me {
  121. display: inline-block;
  122. padding: 13px 12px;
  123. box-sizing: border-box;
  124. position: relative;
  125. width: 100%;
  126. }
  127. .chat_room .content .me .img {
  128. width: 39px;
  129. height: 39px;
  130. background: #ddd;
  131. border-radius: 55px;
  132. float: right;
  133. cursor: pointer;
  134. overflow: hidden;
  135. }
  136. .chat_room .content .me .img img {
  137. width: 100%;
  138. height: 100%;
  139. }
  140. .chat_room .content .me p.name {
  141. float: right;
  142. margin: -2px 9px 0 0;
  143. color: #2d2d2d;
  144. font-size: 15px;
  145. max-width: 150px;
  146. overflow: hidden;
  147. word-break: keep-all;
  148. }
  149. .chat_room .content .me p.content {
  150. float: right;
  151. border: 0;
  152. margin: -8px 49px 0 0;
  153. width: auto;
  154. max-width: 61%;
  155. height: auto;
  156. text-align: left;
  157. word-wrap: break-word;
  158. background: rgb(38, 131, 245);
  159. border-radius: 9px;
  160. box-sizing: border-box;
  161. padding: 9px 17px;
  162. color: #f4f4f4;
  163. }
  164. .chat_room .content .me p.content span {
  165. display: block;
  166. clear: both;
  167. width: 100%;
  168. text-align: right;
  169. color: #f4f4f4;
  170. margin-top: 8px;
  171. font-size: 13px;
  172. }
  173. .chat_room .content .other {
  174. display: inline-block;
  175. padding: 13px 12px;
  176. box-sizing: border-box;
  177. position: relative;
  178. width: 100%;
  179. }
  180. .chat_room .content .other .img {
  181. width: 39px;
  182. height: 39px;
  183. background: #ddd;
  184. border-radius: 55px;
  185. float: left;
  186. cursor: pointer;
  187. overflow: hidden;
  188. }
  189. .chat_room .content .other .img img {
  190. width: 100%;
  191. height: 100%;
  192. }
  193. .chat_room .content .other p.name {
  194. float: left;
  195. margin: -2px 0px 0 10px;
  196. color: #2d2d2d;
  197. font-size: 15px;
  198. max-width: 150px;
  199. overflow: hidden;
  200. word-break: keep-all;
  201. }
  202. .chat_room .content .other p.content {
  203. float: left;
  204. border: 0;
  205. margin: -18px 9px 0 49px;
  206. width: auto;
  207. max-width: 61%;
  208. height: auto;
  209. text-align: left;
  210. word-wrap: break-word;
  211. background: rgb(38, 131, 245);
  212. border-radius: 9px;
  213. box-sizing: border-box;
  214. padding: 9px 17px;
  215. color: #f4f4f4;
  216. }
  217. .chat_room .content .other p.content span {
  218. display: block;
  219. clear: both;
  220. width: 100%;
  221. text-align: left;
  222. color: #f4f4f4;
  223. margin-top: 8px;
  224. font-size: 13px;
  225. }
  226. .setting {
  227. position: fixed;
  228. top: 0;
  229. left: 0;
  230. width: 100%;
  231. height: 100%;
  232. background: rgba(0, 0, 0, 0.67);
  233. }
  234. .setting .setting_main {
  235. width: 471px;
  236. height: 290px;
  237. background: #fff;
  238. margin: auto;
  239. position: relative;
  240. top: 20%;
  241. border-radius: 7px;
  242. box-sizing: border-box;
  243. padding: 16px;
  244. }
  245. .setting .setting_main .img {
  246. width: 90px;
  247. height: 90px;
  248. background: #ddd;
  249. border-radius: 50%;
  250. margin: auto;
  251. margin-top: 10px;
  252. overflow: hidden;
  253. cursor: pointer;
  254. position: relative;
  255. }
  256. .setting .setting_main .img:hover span {
  257. opacity: 1;
  258. }
  259. .setting .setting_main .img img {
  260. width: 100%;
  261. height: 100%;
  262. }
  263. .setting .setting_main .img span {
  264. font-size: 13px;
  265. display: block;
  266. color: #2b2929;
  267. transition: 0.3s;
  268. position: absolute;
  269. top: 32px;
  270. width: 100%;
  271. opacity: 1;
  272. text-align: center;
  273. }
  274. .setting .setting_main .info {
  275. margin-top: 28px;
  276. text-align: center;
  277. }
  278. .setting .setting_main .info span {
  279. margin-right: 10px;
  280. }
  281. .setting .setting_main .info input {
  282. width: 150px;
  283. height: 25px;
  284. border: 1px solid #000;
  285. text-indent: 7px;
  286. }
  287. .setting .setting_main .ok {
  288. width: 248px;
  289. height: 39px;
  290. display: block;
  291. border: 1px solid #333333;
  292. color: #616266;
  293. margin: auto;
  294. cursor: pointer;
  295. background: #fff;
  296. margin-top: 20px;
  297. }
  298. .prompt {
  299. font-size: 55px;
  300. color: #f00;
  301. position: absolute;
  302. top: -45px;
  303. right: 8px;
  304. }
  305. .chat_room .user_list {
  306. position: absolute;
  307. top: -1px;
  308. right: -154px;
  309. width: 152px;
  310. height: 65%;
  311. box-sizing: border-box;
  312. /* padding: 5px 10px; */
  313. background: #eee;
  314. }
  315. .chat_room .user_list .title {
  316. padding: 10px 10px;
  317. }
  318. .chat_room .user_list .list {
  319. width: 100%;
  320. height: calc(100% - 41px);
  321. overflow: auto;
  322. }
  323. .userLabel {
  324. width: 99%;
  325. height: 45px;
  326. line-height: 45px;
  327. font-size: 12px;
  328. overflow: hidden;
  329. border: 1px solid #f1a2a2;
  330. cursor: pointer;
  331. }
  332. </style>
  333. <script>
  334. // ajax请求
  335. function ajax(methods, url, callback, timeout, data) {
  336. var xmlhttp = new XMLHttpRequest();
  337. xmlhttp.open(methods, url, true);
  338. xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  339. xmlhttp.timeout = timeout;
  340. xmlhttp.onreadystatechange = function () {
  341. if (xmlhttp.readyState == 4) {
  342. if (callback) {
  343. callback(xmlhttp.status + xmlhttp.responseText);
  344. }
  345. }
  346. };
  347. if (data) {
  348. xmlhttp.send(data);
  349. } else {
  350. xmlhttp.send();
  351. }
  352. }
  353. // 生成guid
  354. function getGuid() {
  355. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  356. var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
  357. return v.toString(16);
  358. });
  359. }
  360. // 获取cookies
  361. function getCookie(c_name) {
  362. if (document.cookie.length > 0) {
  363. c_start = document.cookie.indexOf(c_name + "=")
  364. if (c_start != -1) {
  365. c_start = c_start + c_name.length + 1
  366. c_end = document.cookie.indexOf(";", c_start)
  367. if (c_end == -1) c_end = document.cookie.length
  368. return unescape(document.cookie.substring(c_start, c_end))
  369. }
  370. }
  371. return ""
  372. }
  373. // 设置cookies
  374. function setCookie(c_name, value, expiredays) {
  375. var exdate = new Date()
  376. exdate.setDate(exdate.getDate() + expiredays)
  377. document.cookie = c_name + "=" + escape(value) +
  378. ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString())
  379. }
  380. // 检验cookies
  381. function checkCookie(cookie) {
  382. var acount = getCookie(cookie)
  383. if (acount != null && acount != "") {
  384. return acount
  385. }
  386. else {
  387. return false
  388. }
  389. }
  390. // 轮询
  391. function ajaxpost(acount) {
  392. ajax("post", 'http://localhost:1347/polling', function (val) {
  393. if (val !== "0") {
  394. val = JSON.parse(val.split('200')[1])
  395. if (Object.prototype.toString.call(val) === "[object Array]") {
  396. updateUserList(val)
  397. } else {
  398. addDom('other', val, val.send)
  399. }
  400. }
  401. ajaxpost(acount)
  402. }, 25000, acount)
  403. }
  404. // 发送
  405. function send() {
  406. if (sendTarget === "" && document.querySelector(".userLabel") !== null) {
  407. sendTarget = document.querySelector(".userLabel").innerText
  408. }
  409. var msg = document.querySelector('.input_content').value
  410. if (sendTarget === "" || msg.trim() === "") {
  411. return
  412. }
  413. document.querySelector('.input_content').value = ''
  414. var DATE = new Date()
  415. var date = DATE.getHours() + ":" + DATE.getMinutes() + ":" + DATE.getSeconds();
  416. var message = {
  417. send: getCookie('acount'),
  418. rece: sendTarget,
  419. msg: msg,
  420. date: date
  421. }
  422. ajax("post", 'http://localhost:1347/send', function (val) {
  423. console.log(val)
  424. }, 0,JSON.stringify(message))
  425. addDom('me', message, message.rece)
  426. }
  427. // 显示发送的消息
  428. function addDom(cla, message, id) {
  429. var classname = cla;
  430. var html = '<div class="' + classname + '">\
  431. <p class="content">\
  432. '+ message.msg + '\
  433. <span class="date">'+ message.date + '</span>\
  434. </p></div>';
  435. document.getElementById(id).innerHTML += html
  436. }
  437. // 获取在线用户
  438. function getUser() {
  439. ajax("get", 'http://localhost:1347/getUser', function () {
  440. }, 0)
  441. }
  442. // 获取未读消息
  443. function getUnreadMsg() {
  444. var acount = getCookie('acount')
  445. ajax("POST", 'http://localhost:1347/getUnread', function (val) {
  446. if (val !== "200") {
  447. val = JSON.parse(val.split('200')[1])
  448. val.forEach(function(i) {
  449. addDom('other', i, i.send)
  450. })
  451. }
  452. }, 0, acount)
  453. }
  454. // 更新用户列表
  455. function updateUserList(userList) {
  456. var acount = getCookie('acount')
  457. document.querySelectorAll('.list')[1].innerHTML = ''
  458. userList.forEach(function(ele) {
  459. if (acount != ele) {
  460. var div = document.createElement('div');
  461. div.className = 'userLabel';
  462. div.innerText = ele
  463. document.querySelectorAll('.list')[1].appendChild(div)
  464. if (!document.getElementById(ele)) {
  465. var content = document.createElement('div');
  466. content.className = 'content';
  467. content.id = ele
  468. document.querySelector('.chat_room').insertBefore(content, document.querySelector('#all'))
  469. }
  470. div.onclick = function() {
  471. document.querySelectorAll('div.content').forEach(function(ele) {
  472. ele.style.display = 'none'
  473. })
  474. document.getElementById(ele).style.display = "block"
  475. }
  476. }
  477. });
  478. }
  479. window.onload = function () {
  480. var acount = checkCookie('acount');
  481. if (acount) {
  482. ajaxpost(acount)
  483. } else {
  484. var guid = getGuid()
  485. setCookie('acount', guid)
  486. ajaxpost(guid)
  487. }
  488. getUser()
  489. addClickEvent()
  490. getUnreadMsg()
  491. }
  492. // 设置发送的用户
  493. var sendTarget = ''
  494. function addClickEvent() {
  495. var list = document.querySelectorAll('.list')[1]
  496. list.addEventListener('click', function(e) {
  497. if (e.target.className === "userLabel") {
  498. sendTarget = e.target.innerText
  499. }
  500. })
  501. }
  502. </script>
  503. </head>
  504. <body>
  505. <div class="chat_room">
  506. <div class="list">
  507. <div class="list_1 on" id="">
  508. <div class="img">
  509. <img src="" alt="">
  510. </div>
  511. <div class="title">公共聊天室</div>
  512. <div class="info"></div>
  513. </div>
  514. </div>
  515. <div class="content" id="all"></div>
  516. <div class="input">
  517. <textarea class="input_content"></textarea>
  518. <input type="button" value="发送" onclick="send()">
  519. </div>
  520. <div class="user_list">
  521. <div class="title">用户列表</div>
  522. <div class="list">
  523. </div>
  524. </div>
  525. </div>
  526. </body>
  527. </html>