用Python写了个websocket即时聊天网页(含客户端、服务端代码)

其他的不说,原理自己搜下,直接上代码


客户端代码:

<html>  
<head>  
<title>WebSocket</title>
    <script src="jquery路径" type="text/javascript"></script>
    <style>
        *{font-family: "微软雅黑"; margin: 0; padding: 0;}

html,
body{width: 100%; height: 100%; background: #000; position: relative;}

.msgInfo{position: fixed; right: 2%; top: 2%; height: 96%; width: 20%; background: #666;}
.msgInfo .hd{width: 100%; text-align: center; height: 40px; line-height: 40px; color: #fff; background: #333;}
.msgInfo .bd{ padding: 20px; height: 100%; overflow: auto; color: #fff; line-height: 30px;}
.msgInfo .bd p{margin: 5px 0;}

.sendBox{position: fixed; left: 2%; bottom: 2%; width: 74%; height: 10%; background: #666; overflow: hidden;}
.sendBox textarea{width: 90%; height: 100%; display: inline-block; background: #666; color: #fff; border: none; float: left;}
.sendBox button{width: 10%; height: 100%; display: inline-block; background: #333; cursor: pointer; color: #fff; border: none; float: left;}

.renBox{margin-left: 2%; margin-top: 2%; width: 74%; height: 84%; position: relative; overflow: hidden; float: left;}
.renBox .ren{display: inline-block; position: absolute; left: 500px; top: 500px;}
.renBox .ren .renHead{ display: inline-block; width: 20px; height: 20px; border-radius: 10px; background: #fff;}
.renBox .ren .sayInfo{color: #fff; position: absolute; top: -30px; width: 200px; left: -90px; text-align: center;}
.renBox div{color: #fff; margin-left: 100%; width: 100%; overflow: hidden; height: 30px;line-height: 30px; font-size: 20px;}


.firstStep{position: fixed; width: 100%; height: 100%; z-index: 999; background: #000; text-align: center;}
.firstStep .firstName{display: inline-block; padding-top: 300px;}
.firstStep .firstName .username{width: 300px; height: 40px; border: 0; padding: 0 10px;}
.firstStep .firstName .btn{width: 100px; height: 40px; border: 0; background: #333; color: #fff; cursor: pointer;}
    </style>
<script>  
var socket;  
   
function init(username){
  var host = "ws://localhost:11011/name/"+username;
  try{  
    socket = new WebSocket(host);  
    socket.onopen    = function(msg){
        log('您已经进入聊天室'
  • 8
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
### 回答1: 使用websocket服务端代码,需要用到以下几个步骤:1. 创建websocket实例,用来连接服务端客户端;2. 添加服务端的回调函数,当客户端发送消息时,服务端就会收到消息;3. 向客户端发送消息;4. 关闭websocket连接。使用websocket客户端代码,需要用到以下几个步骤:1. 创建websocket实例,用来连接服务端客户端;2. 添加客户端的回调函数,当服务端发送消息时,客户端就会收到消息;3. 向服务端发送消息;4. 关闭websocket连接。 ### 回答2: 使用WebSocket服务端客户端代码是一种实现实时双向通信的方式。以下是一个简单的示例: 服务端代码: ```python import asyncio import websockets async def handle(websocket, path): while True: message = await websocket.recv() print(f"Received message: {message}") await websocket.send(f"Server received: {message}") async def start_server(): server = await websockets.serve(handle, "localhost", 8000) print("Server started on ws://localhost:8000") await server.wait_closed() asyncio.run(start_server()) ``` 客户端代码: ```python import asyncio import websockets async def send_message(message): async with websockets.connect("ws://localhost:8000") as websocket: await websocket.send(message) response = await websocket.recv() print(f"Received response: {response}") asyncio.run(send_message("Hello server!")) ``` 在服务端代码中,我们使用`websockets`模块创建一个WebSocket服务器,并定义了一个`handle`函数来处理接收到的消息。该函数通过`await websocket.recv()`接收客户端发送的消息,并使用`await websocket.send()`向客户端发送响应消息。 在客户端代码中,我们使用`websockets`模块创建一个WebSocket连接,并使用`await websocket.send()`向服务端发送消息。使用`await websocket.recv()`接收服务端的响应消息,并输出到控制台。 以上是一个简单的WebSocket服务端客户端代码示例,可以通过运行这两个代码来实现双向通信。 ### 回答3: 使用WebSocket服务端客户端代码如下: 服务端代码: ``` import asyncio import websockets # 定义WebSocket服务器的处理逻辑 async def server_handler(websocket, path): # 接收客户端的消息 while True: message = await websocket.recv() print(f"收到消息:{message}") # 发送消息给客户端 response = f"已收到你的消息:{message}" await websocket.send(response) # 启动WebSocket服务器 start_server = websockets.serve(server_handler, 'localhost', 8000) # 开始事件循环 asyncio.get_event_loop().run_until_complete(start_server) asyncio.get_event_loop().run_forever() ``` 客户端代码: ``` import asyncio import websockets # 定义WebSocket客户端的处理逻辑 async def client_handler(): async with websockets.connect('ws://localhost:8000') as websocket: # 发送消息给服务端 message = input("请输入消息:") await websocket.send(message) print(f"发送消息:{message}") # 接收服务端的消息 response = await websocket.recv() print(f"收到服务端的响应:{response}") # 执行WebSocket客户端逻辑 asyncio.get_event_loop().run_until_complete(client_handler()) ``` 以上是一个简单的WebSocket服务端客户端代码示例。服务端使用`websockets`库创建一个WebSocket服务器,通过定义`server_handler()`函数来处理来自客户端的消息,并将响应返回给客户端客户端使用`websockets`库建立与服务端的连接,通过定义`client_handler()`函数来向服务端发送消息,并接收服务端的响应。请注意,服务端客户端代码分别运行在不同的程序中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值