当前位置:   article > 正文

【Web】html+css+js实现post简易聊天室_html聊天室

html聊天室

1、简述

因为项目需求,就做了一个简易的聊天室,用户输入一句话,通过http-post请求简单的回复一些信息给用户。

2、效果图

在这里插入图片描述

3、核心代码讲解

A、把具体问题通过post请求上传到远程服务器

	function toChat2(enterpriseID,platform, query, source,userStatus,username){
		var url = "http://10.0.30.15:8092/SmartServer/robot/chat"; 
		xhr.onreadystatechange = toPostProcess;
		xhr.open("post", url, true);  
		xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");  
		xhr.send(JSON.stringify({
			"enterpriseID": enterpriseID,
			"platform": platform,
			"query": query,
			"source": source,
			"userStatus": userStatus,
			"username": username 
		})); 
		console.log("ok"); 
	} 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

文中的url是内网的ip地址,你们可以换成你们的url地址。
先设置好参数,参数作用:

参数含义
enterpriseID企业ID
platform平台
query具体问题
source来源
userStatus状态
username名字

B、解析服务器返回的答复,并插入到聊天框

	function toPostProcess() {/*设置当获XHR对象获取到返回信息后执行以下代码*/ 
		if (xhr.readyState === 4) { 
			if (xhr.status === 200) {
				res = xhr.responseText;
				var obj = eval ("(" + res + ")");
				console.log("res ok:" +res); 
				try {   
					var textReply = obj.result.content; 
					str = '<div class="atalk"><span>' + textReply +'</span></div>';   
					// 将之前的内容与要发的内容拼接好 提交
					Words.innerHTML = Words.innerHTML + str; 
					// 置空
					TalkWords.value = "";
					// 滑动到最底部
					Words.scrollTop = Words.scrollHeight;
				} catch (e) {
					return handleError(xhr, e);
				}  
			} else {
				console.log("可能存在跨域问题,解决即可"); 
			}  
		} 
	}	
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

通过Words.innerHTML的形式,将产生的回复信息< div >标签插入到聊天框种,并把输入框置空,然后把滑动条滑动到最下面。就完成了一个简单的聊天。

4、源码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>简易聊天室</title>
    <style type="text/css">
        
		@media screen and (min-width: 320px) and (max-width: 1156px){
			.talk_con_mob{
				width:600px;
				height:500px;
				border:1px solid #666;
				margin:50px auto 0;
				background:#f9f9f9;
			}
			.talk_show_mob{
				width:580px;
				height:420px;
				border:1px solid #666;
				background:#fff;
				margin:10px auto 0;
				overflow:auto;
			}
			.talk_input_mob{
				width:580px;
				margin:10px auto 0;
			}
			.talk_word_mob{
				width:420px;
				height:26px;
				padding:0px;
				float:left;
				margin-left:10px;
				outline:none;
				text-indent:10px;
			}  
		}
			.talk_con{
				width:600px;
				height:500px;
				border:1px solid #666;
				margin:50px auto 0;
				background:#f9f9f9;
			}
			.talk_show{
				width:580px;
				height:420px;
				border:1px solid #666;
				background:#fff;
				margin:10px auto 0;
				overflow:auto;
			}
			.talk_input{
				width:580px;
				margin:10px auto 0;
			}
			.whotalk{
				width:80px;
				height:30px;
				float:left;
				outline:none;
			}
			.talk_word{
				width:420px;
				height:26px;
				padding:0px;
				float:left;
				margin-left:10px;
				outline:none;
				text-indent:10px;
			}         
			.talk_sub{
				width:56px;
				height:30px;
				float:right;
				margin-right:10px;
			}
			.atalk{
			   margin:10px; 
			}
			.atalk span{
				display:inline-block;
				background:#0181cc;
				border-radius:10px;
				color:#fff;
				padding:5px 10px;
			}
			.btalk{
				margin:10px;
				text-align:right;
			}
			.btalk span{
				display:inline-block;
				background:#ef8201;
				border-radius:10px;
				color:#fff;
				padding:5px 10px;
			} 
    </style>
    <script type="text/javascript">   
		var Words;
		var TalkWords;
        var TalkSub;
		var xhr;
		
		function InputPress() {
			if (event.keyCode == 13) { 
				chatRoom();
			}	
		} 
		
		function chatRoom(){
			//定义空字符串
			var str = "";
			if(TalkWords.value == ""){
				 // 消息为空时弹窗
				alert("消息不能为空");
				return;
			}
			str = '<div class="btalk"><span>' + TalkWords.value +'</span></div>' ;  
			
			toChat2("10000000051", "inner", TalkWords.value , "wx", "", "oXFEUwvf_exNy6VLr9nGrxrvAPxQ");
			
			// 将之前的内容与要发的内容拼接好 提交
			Words.innerHTML = Words.innerHTML + str;
		}
		
		function toChat2(enterpriseID,platform, query, source,userStatus,username){
			var url = "http://10.0.30.15:8092/SmartServer/robot/chat"; 
			xhr.onreadystatechange = toPostProcess;
			xhr.open("post", url, true);  
			xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");  
			xhr.send(JSON.stringify({
				"enterpriseID": enterpriseID,
				"platform": platform,
				"query": query,
				"source": source,
				"userStatus": userStatus,
				"username": username 
			})); 
			console.log("ok"); 
		} 
		
		function toPostProcess() {/*设置当获XHR对象获取到返回信息后执行以下代码*/ 
			if (xhr.readyState === 4) { 
				if (xhr.status === 200) {
					res = xhr.responseText;
					var obj = eval ("(" + res + ")");
					console.log("res ok:" +res); 
					try {   
						var textReply = obj.result.content; 
						str = '<div class="atalk"><span>' + textReply +'</span></div>';   
						// 将之前的内容与要发的内容拼接好 提交
						Words.innerHTML = Words.innerHTML + str; 
						
						// 置空
						TalkWords.value = "";
						// 滑动到最底部
						Words.scrollTop = Words.scrollHeight;
					} catch (e) {
						return handleError(xhr, e);
					}  
				} else {
					console.log("可能存在跨域问题,解决即可"); 
				}  
			} 
		}		
		
		function GetXmlHttpObject() {
			var xmlHttp=null; 
			try {
				// Firefox, Opera 8.0+, Safari
				console.log("XMLHttpRequest"); 
				 xmlHttp=new XMLHttpRequest();
			} catch (e) {
				// Internet Explorer
				try {
					console.log("Msxml2"); 
					xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
					console.log("Microsoft"); 
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				}
			}
			return xmlHttp;
		}
		
		
		//is moblie
		var sUserAgent = navigator.userAgent.toLowerCase();
		var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
		var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
		var bIsMidp = sUserAgent.match(/midp/i) == "midp";
		var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
		var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
		var bIsAndroid = sUserAgent.match(/android/i) == "android";
		var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
		var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";

		
		
        window.onload = function(){  
			Words = document.getElementById("words"); 
			TalkWords = document.getElementById("talkwords");
			TalkSub = document.getElementById("talksub"); 
			xhr = new GetXmlHttpObject(); 
            TalkSub.onclick = function(){ 
				chatRoom();			
            }   
			//is mobile
			if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {
				console.log("手机");  
				document.getElementById('talk_con_id').className = 'talk_con_mob';		
				document.getElementById('words').className = 'talk_show_mob';	
				document.getElementById('talk_input_id').className = 'talk_input_mob';	
				document.getElementById('talkwords').className = 'talk_word_mob';	
			} else {
				console.log("电脑"); 
				document.getElementById('talk_con_id').className = 'talk_con';
				document.getElementById('words').className = 'talk_show';	
				document.getElementById('talk_input_id').className = 'talk_input';	
				document.getElementById('talkwords').className = 'talk_word';	
			}
        }
    </script>
</head>
<body>
    <div class="talk_con" id="talk_con_id">
        <div class="talk_show" id="words">
            <div class="atalk"><span id="asay">欢迎进入模拟客户聊天</span></div>
            <!--- div class="btalk"><span id="bsay">用户说:还没呢,你呢?</span></div --->
        </div>
        <div class="talk_input"  id="talk_input_id">>
            <input type="text" class="talk_word" id="talkwords" onkeypress="InputPress()">
            <input type="button" value="发送" class="talk_sub" id="talksub" >
        </div>
    </div>
</body>
</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
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242

读者可自行修改样式,把这个简易的聊天室做的更完美。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/417964
推荐阅读
相关标签
  

闽ICP备14008679号