聊天室发言用javaweb啥实现

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了聊天室发言用javaweb啥实现相关的知识,希望对你有一定的参考价值。

参考技术A <?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Apache Tomcat WebSocket Examples: Chat</title>
<style type="text/css"><![CDATA[
input#chat
width: 410px


#console-container
width: 400px;


#console
border: 1px solid #CCCCCC;
border-right-color: #999999;
border-bottom-color: #999999;
height: 170px;
overflow-y: scroll;
padding: 5px;
width: 100%;


#console p
padding: 0;
margin: 0;

]]></style>
<script type="application/javascript"><![CDATA[
"use strict";

var Chat = ;

Chat.socket = null;

Chat.connect = (function(host)
if ('WebSocket' in window)
Chat.socket = new WebSocket(host);
else if ('MozWebSocket' in window)
Chat.socket = new MozWebSocket(host);
else
Console.log('Error: WebSocket is not supported by this browser.');
return;


Chat.socket.onopen = function ()
Console.log('Info: WebSocket connection opened.');
document.getElementById('chat').onkeydown = function(event)
if (event.keyCode == 13)
Chat.sendMessage();

;
;

Chat.socket.onclose = function ()
document.getElementById('chat').onkeydown = null;
Console.log('Info: WebSocket closed.');
;

Chat.socket.onmessage = function (message)
Console.log(message.data);
;
);

Chat.initialize = function()
if (window.location.protocol == 'http:')
Chat.connect('ws://' + window.location.host + '/websocket/chat');
else
Chat.connect('wss://' + window.location.host + '/websocket/chat');

;

Chat.sendMessage = (function()
var message = document.getElementById('chat').value;
if (message != '')
Chat.socket.send(message);
document.getElementById('chat').value = '';

);

var Console = ;

Console.log = (function(message)
var console = document.getElementById('console');
var p = document.createElement('p');
p.style.wordWrap = 'break-word';
p.innerHTML = message;
console.appendChild(p);
while (console.childNodes.length > 25)
console.removeChild(console.firstChild);

console.scrollTop = console.scrollHeight;
);

Chat.initialize();

document.addEventListener("DOMContentLoaded", function()
// Remove elements with "noscript" class - <noscript> is not allowed in XHTML
var noscripts = document.getElementsByClassName("noscript");
for (var i = 0; i < noscripts.length; i++)
noscripts[i].parentNode.removeChild(noscripts[i]);

, false);

]]></script>
</head>
<body>
<div class="noscript"><h2 style="color: #ff0000">Seems your browser doesn't support Javascript! Websockets rely on Javascript being enabled. Please enable
Javascript and reload this page!</h2></div>
<div>
<p>
<input type="text" placeholder="type and press enter to chat" id="chat" />
</p>
<div id="console-container">
<div id="console"/>
</div>
</div>
</body>
</html>

客户端的代码也是很简单,就是载入页面的时候,创建跟服务器的WebSocket连接。
Chat.connect('ws://' + window.location.host + '/websocket/chat');

然后就是发送信息,接收信息了。

完成上述代码之后,就可以部署了。这里我使用的servlet容器是tomcat 8。以下是我的配置:
<Context path="/websocket" docBase="/Users/cevin/Documents/workspace/tomcat_websocket_chat/web" reloadable="true"/>

部署结束,启动tomcat,访问:http://localhost:8080/websocket/chat.xhtml,见到下面这个页面,说明部署成功了。本回答被提问者和网友采纳
参考技术B 用ajax轮询访问吧。

用python做些有意思的事——分析QQ聊天记录(补充)

    之前,写了这篇文章,用python提取全部群成员的发言时间,并简单做了下分析。先补充一下,针对特定单个群成员(这里以  小小白   为例)消息记录的获取。

代码比较简单,主要是正则表达式的书写。(附:聊天文件记录的导出请参考上面提到的文章)

    代码如下:

#2016/9/14
#从QQ聊天数据导出特定人发言的日期时间和发言内容

import re
import xlsxwriter

# 小小白   这里代指你要获取数据的对象的昵称
# 方便起见,见数据导出的文件名也明明为此
workbook = xlsxwriter.Workbook(小小白.xlsx)
worksheet = workbook.add_worksheet()
worksheet.set_column(A:A, 5)
worksheet.set_column(B:B, 10)
worksheet.set_column(C:C, 200)

with open(高等数学.txt,encoding=utf-8) as f:
    s = f.read()
    # 正则,跨行匹配
    pa = re.compile(r^(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2}) 小小白\(小小白的QQ号\)\n(.*?)\n$,re.DOTALL+re.MULTILINE)
    ma = re.findall(pa,s)
    # print(len(ma))
    for i in range(len(ma)):
        # print(ma[i][0])
        date = ma[i][0]
        time = ma[i][1]
        word = ma[i][2]

        worksheet.write(int(i),0,date)
        worksheet.write(int(i),1,time)
        worksheet.write(int(i),2,word)

    workbook.close()
    print("处理完毕,快去看看文件夹下面新建的.xlsx文件吧")

 

以上是关于聊天室发言用javaweb啥实现的主要内容,如果未能解决你的问题,请参考以下文章

java 网络编程中 tcp连接问题。 例如编写聊天室 , c/s结构的c和s端都是不间断实现请求--响应 。

web聊天功能如何设计?

javaweb与websocket实现在线聊天功能总结

在直播APP制作时如何设置直播软件聊天发言部分可见的功能

基于javaweb项目的SSM即时在线聊天简易系统.rar

workerman用啥语言实现的