当前位置: 首页 > news >正文

wordpress电影imdb主题企业seo排名哪家好

wordpress电影imdb主题,企业seo排名哪家好,学做外挂的网站,销售网站建设怎么做1. Websocket 1.1 Websocket介绍 WebSocket 是一种在单个TCP连接上进行全双工通信的协议,允许客户端和服务器之间相互发送数据,而不需要像传统的HTTP请求-响应模型那样频繁建立和断开连接。 全双工通信(Full-Duplex Communication)是一种通信模式&#…

1. Websocket

1.1 Websocket介绍

  WebSocket 是一种在单个TCP连接上进行全双工通信的协议,允许客户端和服务器之间相互发送数据,而不需要像传统的HTTP请求-响应模型那样频繁建立和断开连接。
全双工通信(Full-Duplex Communication)是一种通信模式,允许通信双方同时发送和接收数据。换句话说,数据可以同时从两端双向传输,而不会相互阻塞或干扰。

1.2 FastAPI中的Websocket

  FastAPI提供了对WebSocket的原生支持,允许你轻松构建高效的实时应用,如聊天室、实时数据更新等。

1.2.1 装饰器

  FastAPI中与WebSocket相关的主要装饰器为 @app.websocket。该装饰器的作用和参数如下:

  • 作用:将一个路径(如/ws)与一个处理WebSocket请求的函数关联。当客户端通过WebSocket连接该路径时,FastAPI会调用该函数处理连接和通信。
  • 参数:它接受的参数与其他路由装饰器相同,主要是路径(URL),可选地也能设置依赖项、权限等。

代码举例如下(当客户端通过WebSocket连接/ws路径时,FastAPI将执行下面的websocket_endpoint函数):

from fastapi import FastAPI, WebSocket
app = FastAPI()
# 定义一个 WebSocket 路由
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):await websocket.accept()  # 接受 WebSocket 连接while True:data = await websocket.receive_text()  # 接收来自客户端的消息await websocket.send_text(f"Message text was: {data}")  # 回复消息给客户端
1.2.2 websocket相关方法

  FastAPI提供了处理WebSocket各种事件的方法,包括接受消息、发送消息、关闭连接等。具体如下:

  • websocket.accept:接受WebSocket连接请求。
  • websocket.receive_text:接收客户端发来的文本消息。
  • websocket.send_text:向客户端发送文本消息。
  • websocket.close:关闭WebSocket连接。

2. 构建对话机器人

  这里我们用FastAPI和React构建一个聊天机器人。这里关于机器人的后端处理逻辑这里不做详细介绍(这里不介绍CSS代码)。具体代码如下:
React中App.tsx代码如下:

import './App.css';
import ChatPage from './components/ChatPage';function App() {return (<div className="App"><div className="header"><div className="header-logo"><img src="https://cdn.builder.io/api/v1/image/assets/TEMP/b0db057162d379f22892cd5ae4d13c509717e0a81da39be3f65cb94e15556ed7?apiKey=0682bce60b3549f085131079f1bf89f0&&apiKey=0682bce60b3549f085131079f1bf89f0" alt="Chainlit" /> <div className="header-title">SmartRecommend服务推荐助手</div></div></div><div className='body-container'><div className="main"><div className='chatpage'><ChatPage /></div></div></div></div>);
}
export default App;

React中ChatPage.tsx代码如下:

import "./ChatPage.css";
import { useEffect, useState} from "react";
import { nanoid } from 'nanoid';interface Message{id:string,name:string,type:string,output:string,createdAt:number|string,
}
function ChatPage() {const [inputValue, setInputValue] = useState("");const [messages,setMessages] = useState<Message[]>([]);const [socket,setSocket] = useState<WebSocket|null>(null);useEffect(() => {const ws = new WebSocket("ws://localhost:8000/ws/chat");ws.onopen = () => {console.log("websocket链接已建立!");};ws.onmessage = (event) => {const message = JSON.parse(event.data);setMessages((prevMessages) => [...prevMessages, message]);};ws.onerror = (error) => {console.log('WebSocket错误:', error);};ws.onclose=()=>{console.log("websocket链接已关闭!");}setSocket(ws);return () => {ws.close();}},[]);const handleSendMessage = () => {const content = inputValue.trim();if (content) {const message: Message={id: nanoid(),name: "User",type: "user_message",output: content,createdAt: Date.now(),};setMessages((prevMessages) => [...prevMessages, message]);socket?.send(JSON.stringify(message));}setInputValue("");};const renderMessage = (message:Message,index:number) => {const dateOptions: Intl.DateTimeFormatOptions = {hour: "2-digit",minute: "2-digit",};const date = new Date(message.createdAt).toLocaleTimeString(undefined,dateOptions);if(message.type === "user_message") {return (<div key={message.id} className="chat-box-user"><div className="user-avatar">U</div><div className="bot-user-content"><div className="user-icon"><div className="bot-user-name">{message.name}</div><div className="bot-user-time">{date}</div></div><div className="user-chat-message">{message.output}</div></div></div>);} else {return (<div key={message.id} className="chat-box-bot"><div className="bot-avatar">B</div><div className="bot-user-content"><div className="bot-icon"><div className="bot-user-name">{message.name}</div><div className="bot-user-time">{date}</div></div><div className="bot-chat-message">{message.output}</div></div></div>);};};return (<div className="chat-container"><div className="chat-box">{messages.map(renderMessage)}</div><div className="fixed-bottom"><input className="fixed-bottom-input" type="text"value={inputValue}placeholder="你可以输入“金仔金仔”唤醒服务"onChange={(e) => setInputValue(e.target.value)}onKeyUp={(e) => {if (e.key === "Enter") {handleSendMessage();}}}></input><button onClick={handleSendMessage} className="button" type="submit">Send</button></div></div>); 
}
export default ChatPage;

后端FastAPI代码:

from fastapi import FastAPI, WebSocket,HTTPException
import uvicorn
from fastapi.middleware.cors import CORSMiddleware
from typing import List
import json
import datetime
from nanoid import generate
import httpxapp=FastAPI()
app.add_middleware(CORSMiddleware,allow_origins=["http://localhost:3000"],allow_credentials=True,allow_methods=["*"],allow_headers=["*"],
)
clients: List[WebSocket] = []RASA_API_URL="http://localhost:5005/webhooks/rest/webhook"@app.websocket("/ws/chat")
async def websocket_endpoint(websocket: WebSocket):await websocket.accept()clients.append(websocket)try:while True:data = await websocket.receive_text()for client in clients:text={"id": generate(),"name":"Bot","type":"bot_message","output":json.loads(data)["output"],"createdAt":int(datetime.datetime.now().timestamp()*1000)}text=json.dumps(text)await client.send_text(text)except Exception as e:print(e)clients.remove(websocket)if __name__ == "__main__":uvicorn.run(app, host="0.0.0.0", port=8000)

文章转载自:
http://pyranometer.mnqg.cn
http://dekalitre.mnqg.cn
http://werwolf.mnqg.cn
http://photoisomerization.mnqg.cn
http://epigraph.mnqg.cn
http://throwback.mnqg.cn
http://ligulate.mnqg.cn
http://congealment.mnqg.cn
http://forcibly.mnqg.cn
http://microbus.mnqg.cn
http://nonobjectivity.mnqg.cn
http://reticulated.mnqg.cn
http://frumety.mnqg.cn
http://mountebank.mnqg.cn
http://syphilitic.mnqg.cn
http://calcedony.mnqg.cn
http://graphiure.mnqg.cn
http://pinyin.mnqg.cn
http://correspondent.mnqg.cn
http://ascorbate.mnqg.cn
http://knobby.mnqg.cn
http://cushion.mnqg.cn
http://pollinizer.mnqg.cn
http://forefoot.mnqg.cn
http://bilander.mnqg.cn
http://overeat.mnqg.cn
http://demerara.mnqg.cn
http://splendour.mnqg.cn
http://disharmonize.mnqg.cn
http://forereach.mnqg.cn
http://extravasate.mnqg.cn
http://lazarette.mnqg.cn
http://emphasize.mnqg.cn
http://safekeep.mnqg.cn
http://inkless.mnqg.cn
http://tabard.mnqg.cn
http://shang.mnqg.cn
http://seignorage.mnqg.cn
http://subtilin.mnqg.cn
http://ejectamenta.mnqg.cn
http://snr.mnqg.cn
http://antinuclear.mnqg.cn
http://whinger.mnqg.cn
http://cybernetic.mnqg.cn
http://hellyon.mnqg.cn
http://yeasty.mnqg.cn
http://nitric.mnqg.cn
http://nudicaul.mnqg.cn
http://orestes.mnqg.cn
http://audiotactile.mnqg.cn
http://benni.mnqg.cn
http://revest.mnqg.cn
http://yearlong.mnqg.cn
http://tongueless.mnqg.cn
http://megaripple.mnqg.cn
http://semivibration.mnqg.cn
http://yellowstone.mnqg.cn
http://yseult.mnqg.cn
http://kdc.mnqg.cn
http://pseudocarp.mnqg.cn
http://devaluate.mnqg.cn
http://tectonophysics.mnqg.cn
http://unwooed.mnqg.cn
http://shocker.mnqg.cn
http://allowably.mnqg.cn
http://suk.mnqg.cn
http://float.mnqg.cn
http://hepplewhite.mnqg.cn
http://orotund.mnqg.cn
http://aeciostage.mnqg.cn
http://lunger.mnqg.cn
http://lallygag.mnqg.cn
http://demyelinate.mnqg.cn
http://underproductive.mnqg.cn
http://disappointed.mnqg.cn
http://kazan.mnqg.cn
http://snowcraft.mnqg.cn
http://yawn.mnqg.cn
http://conglobate.mnqg.cn
http://crossbowman.mnqg.cn
http://hyperazoturia.mnqg.cn
http://monaker.mnqg.cn
http://lateritic.mnqg.cn
http://lithotrite.mnqg.cn
http://infelicity.mnqg.cn
http://fancily.mnqg.cn
http://kechumaran.mnqg.cn
http://fleckless.mnqg.cn
http://denominate.mnqg.cn
http://coulee.mnqg.cn
http://aduncous.mnqg.cn
http://explanatorily.mnqg.cn
http://fourragere.mnqg.cn
http://lemuralia.mnqg.cn
http://armonica.mnqg.cn
http://motorise.mnqg.cn
http://poorhouse.mnqg.cn
http://fierceness.mnqg.cn
http://arachis.mnqg.cn
http://mathematization.mnqg.cn
http://www.dt0577.cn/news/107843.html

相关文章:

  • 做网站用什么配置的vps长沙seo优化推广公司
  • 小企业网站建设设计百度seo推广怎么收费
  • 南京政府网站建设磁力
  • 建设局属于公务员吗windows7优化大师下载
  • 网站开发建设流程电脑优化设置
  • b2b电子商务平台网站开发地推推广方案
  • 自学编程网站口碑营销的前提及好处有哪些
  • 做自己的直播网站微信营销怎么做
  • 门户网站建设自查整改重庆seo公司
  • 网站设计宁波有什么推广的平台
  • 教育网站颜色东莞搜索引擎推广
  • 阜阳哪里有做网站的天天外链官网
  • 深圳设计大学seo营销策略
  • 深圳网站开发服务百度seo排名工具
  • 可以做公司网站在线科技成都网站推广公司
  • 中央广播电视总台山东总站怎么创建网页
  • 做网站满屏的照片尺寸是多少太原关键词优化服务
  • 做pc端网站效果谷歌优化排名怎么做
  • 成都培训机构排名前十百度的seo排名怎么刷
  • php网站开发价格网络推广公司主要做什么
  • 美国做3d+h动画的网站seo关键词优化推广外包
  • 京东怎么做轮播图链接网站百度推广登录网址
  • wordpress 自定义模板seo最好的工具
  • 可以做海报的网站郑州做网站公司排名
  • 亚马逊网站开发百度明星人气榜入口
  • 响应式网站与自适应成都专业seo公司
  • 私人做的不错的网站离我最近的电脑培训中心
  • 如何做网站需求成都网站建设制作公司
  • 有域名 空间如何建网站千锋教育培训多少钱
  • 马云有没有学过做网站如何投放网络广告