Open main menu
首页
专栏
课程
分类
归档
Chat
Sci-Hub
谷歌学术
Libgen
GitHub镜像
登录/注册
搜索
搜索
关闭
Previous
Previous
Next
Next
使用chatgpt实现微信聊天小程序(秒回复),github开源(附带链接)
sockstack
/
188
/
2023-12-05 00:03:15
<p><span style="color: red; font-size: 18px">ChatGPT 可用网址,仅供交流学习使用,如对您有所帮助,请收藏并推荐给需要的朋友。</span><br><a href="https://ckai.xyz/?sockstack§ion=detail" target="__blank">https://ckai.xyz</a><br><br></p> <article class="baidu_pl"><div id="article_content" class="article_content clearfix"> <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css"> <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-25cebea3f9.css"> <div id="content_views" class="markdown_views prism-github-gist"> <svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path></svg><p></p> <div class="toc"> <h3>文章目录</h3> <ul> <li>前言</li> <li>效果展示</li> <li>原理说明</li> <li>服务器端代码说明</li> <li>微信小程序代码说明</li> <li>代码链接</li> <li>总结</li> </ul> </div> <p></p> <h1> <a id="_1"></a>前言</h1> <p>我在前一段时间突发奇想,就使用java来调用chatgpt的接口,然后写了一个简单小程序,也上了热榜第一,java调用chatgpt接口,实现专属于自己的人工智能助手,事实上,这个程序毛病挺多的,最不能让人接受的一点就是返回速度非常缓慢(即使使用非常好的外网服务器)。</p> <p>现在,我改进了一下程序,使用异步请求的方式,<strong>基本可以实现秒回复</strong>。并且还基于webSocket编写了一个微信小程序来进行交互,可以直接使用微信小程序来进行体验。</p> <p>现在我将所有代码都上传了github(链接在文章结尾),大家可以clone下来,部署到服务器上,真正实现自己的聊天机器人!!!</p> <hr> <h1> <a id="_9"></a>效果展示</h1> <p>部分截图如下<br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/d7766b7da71544dd95d09bb907fc2a34.png#pic_center" alt="在这里插入图片描述"><br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/2b1d3d85a07943b3aca8993f69f57785.png#pic_center" alt="在这里插入图片描述"></p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/1a7596f875204cfe9d79e680fb427738.png#pic_center" alt="在这里插入图片描述"><br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/3312583420004a36924b225543916da9.png#pic_center" alt="在这里插入图片描述"><br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/872c600c0a6b463b81af07b56bcf8b5f.png#pic_center" alt="在这里插入图片描述"></p> <hr> <h1> <a id="_19"></a>原理说明</h1> <p>在 java调用chatgpt接口,实现专属于自己的人工智能助手 我说明了java调用chatgpt的基本原理,这里的代码就是对这个代码的改进,使用异步请求的方式来进行。</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/5214240c02e0479c90e3f49a424e117e.png" alt="在这里插入图片描述"></p> <p>注意看官方文档,我们在请求时可以提供一个参数stream,然后就可以实现按照流的形式进行返回,这种方式基本可以做到没有延迟就给出答案。</p> <p>由于这次改进的思路主要就是将请求改为了异步,其他的基本一样,所以就不做解释,直接给出代码了,代码上面都有注释</p> <pre><code class="prism language-java"> <span class="token comment">/*** 这个方法用于测试的,可以在控制台打印输出结果** @param chatGptRequestParameter 请求的参数* @param question 问题*/</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">printAnswer</span><span class="token punctuation">(</span><span class="token class-name">ChatRequestParameter</span> chatGptRequestParameter<span class="token punctuation">,</span> <span class="token class-name">String</span> question<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>asyncClient<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 创建一个post请求</span><span class="token class-name">AsyncRequestBuilder</span> asyncRequest <span class="token operator">=</span> <span class="token class-name">AsyncRequestBuilder</span><span class="token punctuation">.</span><span class="token function">post</span><span class="token punctuation">(</span>url<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 设置请求参数</span>chatGptRequestParameter<span class="token punctuation">.</span><span class="token function">addMessages</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">ChatMessage</span><span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">,</span> question<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 请求的参数转换为字符串</span><span class="token class-name">String</span> valueAsString <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span><span class="token keyword">try</span> <span class="token punctuation">{<!-- --></span>valueAsString <span class="token operator">=</span> objectMapper<span class="token punctuation">.</span><span class="token function">writeValueAsString</span><span class="token punctuation">(</span>chatGptRequestParameter<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">JsonProcessingException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment">// 设置编码和请求参数</span><span class="token class-name">ContentType</span> contentType <span class="token operator">=</span> <span class="token class-name">ContentType</span><span class="token punctuation">.</span><span class="token function">create</span><span class="token punctuation">(</span><span class="token string">"text/plain"</span><span class="token punctuation">,</span> charset<span class="token punctuation">)</span><span class="token punctuation">;</span>asyncRequest<span class="token punctuation">.</span><span class="token function">setEntity</span><span class="token punctuation">(</span>valueAsString<span class="token punctuation">,</span> contentType<span class="token punctuation">)</span><span class="token punctuation">;</span>asyncRequest<span class="token punctuation">.</span><span class="token function">setCharset</span><span class="token punctuation">(</span>charset<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 设置请求头</span>asyncRequest<span class="token punctuation">.</span><span class="token function">setHeader</span><span class="token punctuation">(</span><span class="token class-name">HttpHeaders</span><span class="token punctuation">.</span>CONTENT_TYPE<span class="token punctuation">,</span> <span class="token string">"application/json"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 设置登录凭证</span>asyncRequest<span class="token punctuation">.</span><span class="token function">setHeader</span><span class="token punctuation">(</span><span class="token class-name">HttpHeaders</span><span class="token punctuation">.</span>AUTHORIZATION<span class="token punctuation">,</span> <span class="token string">"Bearer "</span> <span class="token operator">+</span> apiKey<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 下面就是生产者消费者模型</span><span class="token class-name">CountDownLatch</span> latch <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">CountDownLatch</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 用于记录返回的答案</span><span class="token class-name">StringBuilder</span> sb <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">StringBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 消费者</span><span class="token class-name">AbstractCharResponseConsumer</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">HttpResponse</span><span class="token punctuation">></span></span> consumer <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">AbstractCharResponseConsumer</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">HttpResponse</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token class-name">HttpResponse</span> response<span class="token punctuation">;</span><span class="token annotation punctuation">@Override</span><span class="token keyword">protected</span> <span class="token keyword">void</span> <span class="token function">start</span><span class="token punctuation">(</span><span class="token class-name">HttpResponse</span> response<span class="token punctuation">,</span> <span class="token class-name">ContentType</span> contentType<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">HttpException</span><span class="token punctuation">,</span> <span class="token class-name">IOException</span> <span class="token punctuation">{<!-- --></span><span class="token function">setCharset</span><span class="token punctuation">(</span>charset<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">this</span><span class="token punctuation">.</span>response <span class="token operator">=</span> response<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token annotation punctuation">@Override</span><span class="token keyword">protected</span> <span class="token keyword">int</span> <span class="token function">capacityIncrement</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token keyword">return</span> <span class="token class-name">Integer</span><span class="token punctuation">.</span>MAX_VALUE<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token annotation punctuation">@Override</span><span class="token keyword">protected</span> <span class="token keyword">void</span> <span class="token function">data</span><span class="token punctuation">(</span><span class="token class-name">CharBuffer</span> src<span class="token punctuation">,</span> <span class="token keyword">boolean</span> endOfStream<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">{<!-- --></span><span class="token comment">// 收到一个请求就进行处理</span><span class="token class-name">String</span> ss <span class="token operator">=</span> src<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 通过data:进行分割,如果不进行此步,可能返回的答案会少一些内容</span><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">String</span> s <span class="token operator">:</span> ss<span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">"data:"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token comment">// 去除掉data:</span><span class="token keyword">if</span> <span class="token punctuation">(</span>s<span class="token punctuation">.</span><span class="token function">startsWith</span><span class="token punctuation">(</span><span class="token string">"data:"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>s <span class="token operator">=</span> s<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment">// 返回的数据可能是(DONE)</span><span class="token keyword">if</span> <span class="token punctuation">(</span>s<span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">8</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token comment">// 转换为对象</span><span class="token class-name">ChatResponseParameter</span> responseParameter <span class="token operator">=</span> objectMapper<span class="token punctuation">.</span><span class="token function">readValue</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span> <span class="token class-name">ChatResponseParameter</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 处理结果</span><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">Choice</span> choice <span class="token operator">:</span> responseParameter<span class="token punctuation">.</span><span class="token function">getChoices</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token class-name">String</span> content <span class="token operator">=</span> choice<span class="token punctuation">.</span><span class="token function">getDelta</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getContent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">if</span> <span class="token punctuation">(</span>content <span class="token operator">!=</span> <span class="token keyword">null</span> <span class="token operator">&&</span> <span class="token operator">!</span><span class="token string">""</span><span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>content<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token comment">// 保存结果</span>sb<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span>content<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 将结果使用webSocket传送过去</span><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>content<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token annotation punctuation">@Override</span><span class="token keyword">protected</span> <span class="token class-name">HttpResponse</span> <span class="token function">buildResult</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">{<!-- --></span><span class="token keyword">return</span> response<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token annotation punctuation">@Override</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">releaseResources</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">;</span><span class="token comment">// 执行请求</span>asyncClient<span class="token punctuation">.</span><span class="token function">execute</span><span class="token punctuation">(</span>asyncRequest<span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> consumer<span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">FutureCallback</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">HttpResponse</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token annotation punctuation">@Override</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">completed</span><span class="token punctuation">(</span><span class="token class-name">HttpResponse</span> response<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>latch<span class="token punctuation">.</span><span class="token function">countDown</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>chatGptRequestParameter<span class="token punctuation">.</span><span class="token function">addMessages</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">ChatMessage</span><span class="token punctuation">(</span><span class="token string">"assistant"</span><span class="token punctuation">,</span> sb<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"回答结束!!!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token annotation punctuation">@Override</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">failed</span><span class="token punctuation">(</span><span class="token class-name">Exception</span> ex<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>latch<span class="token punctuation">.</span><span class="token function">countDown</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"failed"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>ex<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token annotation punctuation">@Override</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">cancelled</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>latch<span class="token punctuation">.</span><span class="token function">countDown</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"cancelled"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">try</span> <span class="token punctuation">{<!-- --></span>latch<span class="token punctuation">.</span><span class="token function">await</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token punctuation">}</span> </code></pre> <blockquote> <p>大家代码可以直接不看,反正最终的效果就是可以实现问了问题就返回结果。运行效果如下</p> </blockquote> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/7a410d5599c24baab514b6e5dc2f8f40.png#pic_center" alt="在这里插入图片描述"><br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/31a91d3323f34458aea27c43bc04f337.png#pic_center" alt="在这里插入图片描述"></p> <blockquote> <p>可以发现,输出就类似于官方的那种效果,一个字一个字的输出</p> </blockquote> <hr> <h1> <a id="_155"></a>服务器端代码说明</h1> <p>我使用java搭建了一个简单的服务器端程序,提供最基础的用户登录校验功能,以及提供了WebSocket通信。</p> <p><strong>用户校验的代码</strong></p> <pre><code class="prism language-java"><span class="token keyword">package</span> <span class="token namespace">com<span class="token punctuation">.</span>ttpfx<span class="token punctuation">.</span>controller</span><span class="token punctuation">;</span><span class="token keyword">import</span> <span class="token namespace">com<span class="token punctuation">.</span>ttpfx<span class="token punctuation">.</span>entity<span class="token punctuation">.</span></span><span class="token class-name">User</span><span class="token punctuation">;</span> <span class="token keyword">import</span> <span class="token namespace">com<span class="token punctuation">.</span>ttpfx<span class="token punctuation">.</span>service<span class="token punctuation">.</span></span><span class="token class-name">UserService</span><span class="token punctuation">;</span> <span class="token keyword">import</span> <span class="token namespace">com<span class="token punctuation">.</span>ttpfx<span class="token punctuation">.</span>utils<span class="token punctuation">.</span></span><span class="token class-name">R</span><span class="token punctuation">;</span> <span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>bind<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">RequestMapping</span><span class="token punctuation">;</span> <span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>bind<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">RestController</span><span class="token punctuation">;</span><span class="token keyword">import</span> <span class="token namespace">javax<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">Resource</span><span class="token punctuation">;</span> <span class="token keyword">import</span> <span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token class-name">Objects</span><span class="token punctuation">;</span> <span class="token keyword">import</span> <span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>concurrent<span class="token punctuation">.</span></span><span class="token class-name">ConcurrentHashMap</span><span class="token punctuation">;</span><span class="token comment">/*** @author ttpfx* @date 2023/3/29*/</span> <span class="token annotation punctuation">@RestController</span> <span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/user"</span><span class="token punctuation">)</span> <span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">UserController</span> <span class="token punctuation">{<!-- --></span><span class="token annotation punctuation">@Resource</span><span class="token keyword">private</span> <span class="token class-name">UserService</span> userService<span class="token punctuation">;</span><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">ConcurrentHashMap</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">User</span><span class="token punctuation">></span></span> loginUser <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ConcurrentHashMap</span><span class="token generics"><span class="token punctuation"><</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">ConcurrentHashMap</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">Long</span><span class="token punctuation">></span></span> loginUserKey <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ConcurrentHashMap</span><span class="token generics"><span class="token punctuation"><</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/login"</span><span class="token punctuation">)</span><span class="token keyword">public</span> <span class="token class-name">R</span> <span class="token function">login</span><span class="token punctuation">(</span><span class="token class-name">String</span> username<span class="token punctuation">,</span> <span class="token class-name">String</span> password<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token keyword">if</span> <span class="token punctuation">(</span>username <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token class-name">R</span><span class="token punctuation">.</span><span class="token function">fail</span><span class="token punctuation">(</span><span class="token string">"必须填写用户名"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token class-name">User</span> user <span class="token operator">=</span> userService<span class="token punctuation">.</span><span class="token function">queryByName</span><span class="token punctuation">(</span>username<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">if</span> <span class="token punctuation">(</span>user <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token class-name">R</span><span class="token punctuation">.</span><span class="token function">fail</span><span class="token punctuation">(</span><span class="token string">"用户名不存在"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token class-name">String</span> targetPassword <span class="token operator">=</span> user<span class="token punctuation">.</span><span class="token function">getPassword</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">if</span> <span class="token punctuation">(</span>targetPassword <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token class-name">R</span><span class="token punctuation">.</span><span class="token function">fail</span><span class="token punctuation">(</span><span class="token string">"用户密码异常"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>targetPassword<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>password<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token class-name">R</span><span class="token punctuation">.</span><span class="token function">fail</span><span class="token punctuation">(</span><span class="token string">"密码错误"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>loginUser<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span>username<span class="token punctuation">,</span> user<span class="token punctuation">)</span><span class="token punctuation">;</span>loginUserKey<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span>username<span class="token punctuation">,</span> <span class="token class-name">System</span><span class="token punctuation">.</span><span class="token function">currentTimeMillis</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">return</span> <span class="token class-name">R</span><span class="token punctuation">.</span><span class="token function">ok</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span>loginUserKey<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>username<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/logout"</span><span class="token punctuation">)</span><span class="token keyword">public</span> <span class="token class-name">R</span> <span class="token function">logout</span><span class="token punctuation">(</span><span class="token class-name">String</span> username<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>loginUser<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span>username<span class="token punctuation">)</span><span class="token punctuation">;</span>loginUserKey<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span>username<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">return</span> <span class="token class-name">R</span><span class="token punctuation">.</span><span class="token function">ok</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/checkUserKey"</span><span class="token punctuation">)</span><span class="token keyword">public</span> <span class="token class-name">R</span> <span class="token function">checkUserKey</span><span class="token punctuation">(</span><span class="token class-name">String</span> username<span class="token punctuation">,</span> <span class="token class-name">Long</span> key<span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span><span class="token keyword">if</span> <span class="token punctuation">(</span>username<span class="token operator">==</span><span class="token keyword">null</span> <span class="token operator">||</span> key <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token keyword">return</span> <span class="token class-name">R</span><span class="token punctuation">.</span><span class="token function">fail</span><span class="token punctuation">(</span><span class="token string">"用户校验异常"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token class-name">Objects</span><span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>loginUserKey<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>username<span class="token punctuation">)</span><span class="token punctuation">,</span> key<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span><span class="token keyword">return</span> <span class="token class-name">R</span><span class="token punctuation">.</span><span class="token function">fail</span><span class="token punctuation">(</span><span class="token string">"用户在其他地方登录!!!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">return</span> <span class="token class-name">R</span><span class="token punctuation">.</span><span class="token function">ok</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/loginUser"</span><span class="token punctuation">)</span><span class="token keyword">public</span> <span class="token class-name">R</span> <span class="token function">loginUser</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span><span class="token keyword">return</span> <span class="token class-name">R</span><span class="token punctuation">.</span><span class="token function">ok</span><span class="token punctuation">(</span><span class="token string">"success"</span><span class="token punctuation">,</span>loginUser<span class="token punctuation">.</span><span class="token function">keySet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span> <span class="token punctuation">}</span></code></pre> <p><strong>基于webSocket通信的代码</strong></p> <pre><code class="prism language-java"><span class="token keyword">package</span> <span class="token namespace">com<span class="token punctuation">.</span>ttpfx<span class="token punctuation">.</span>server</span><span class="token punctuation">;</span><span class="token keyword">import</span> <span class="token namespace">com<span class="token punctuation">.</span>fasterxml<span class="token punctuation">.</span>jackson<span class="token punctuation">.</span>databind<span class="token punctuation">.</span></span><span class="token class-name">ObjectMapper</span><span class="token punctuation">;</span> <span class="token keyword">import</span> <span class="token namespace">com<span class="token punctuation">.</span>ttpfx<span class="token punctuation">.</span>entity<span class="token punctuation">.</span></span><span class="token class-name">UserLog</span><span class="token punctuation">;</span> <span class="token keyword">import</span> <span class="token namespace">com<span class="token punctuation">.</span>ttpfx<span class="token punctuation">.</span>model<span class="token punctuation">.</span></span><span class="token class-name">ChatModel</span><span class="token punctuation">;</span> <span class="token keyword">import</span> <span class="token namespace">com<span class="token punctuation">.</span>ttpfx<span class="token punctuation">.</span>service<span class="token punctuation">.</span></span><span class="token class-name">UserLogService</span><span class="token punctuation">;</span> <span class="token keyword">import</span> <span class="token namespace">com<span class="token punctuation">.</span>ttpfx<span class="token punctuation">.</span>service<span class="token punctuation">.</span></span><span class="token class-name">UserService</span><span class="token punctuation">;</span> <span class="token keyword">import</span> <span class="token namespace">com<span class="token punctuation">.</span>ttpfx<span class="token punctuation">.</span>vo<span class="token punctuation">.</span>chat<span class="token punctuation">.</span></span><span class="token class-name">ChatRequestParameter</span><span class="token punctuation">;</span> <span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>stereotype<span class="token punctuation">.</span></span><span class="token class-name">Component</span><span class="token punctuation">;</span><span class="token keyword">import</span> <span class="token namespace">javax<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">Resource</span><span class="token punctuation">;</span> <span class="token keyword">import</span> <span class="token namespace">javax<span class="token punctuation">.</span>websocket<span class="token punctuation">.</span></span><span class="token operator">*</span><span class="token punctuation">;</span> <span class="token keyword">import</span> <span class="token namespace">javax<span class="token punctuation">.</span>websocket<span class="token punctuation">.</span>server<span class="token punctuation">.</span></span><span class="token class-name">PathParam</span><span class="token punctuation">;</span> <span class="token keyword">import</span> <span class="token namespace">javax<span class="token punctuation">.</span>websocket<span class="token punctuation">.</span>server<span class="token punctuation">.</span></span><span class="token class-name">ServerEndpoint</span><span class="token punctuation">;</span> <span class="token keyword">import</span> <span class="token namespace">java<span class="token punctuation">.</span>io<span class="token punctuation">.</span></span><span class="token class-name">IOException</span><span class="token punctuation">;</span> <span class="token keyword">import</span> <span class="token namespace">java<span class="token punctuation">.</span>time<span class="token punctuation">.</span></span><span class="token class-name">LocalDateTime</span><span class="token punctuation">;</span> <span class="token keyword">import</span> <span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>concurrent<span class="token punctuation">.</span></span><span class="token class-name">ConcurrentHashMap</span><span class="token punctuation">;</span><span class="token comment">/*** @author ttpfx* @date 2023/3/28*/</span> <span class="token annotation punctuation">@Component</span> <span class="token annotation punctuation">@ServerEndpoint</span><span class="token punctuation">(</span><span class="token string">"/chatWebSocket/{username}"</span><span class="token punctuation">)</span> <span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ChatWebSocketServer</span> <span class="token punctuation">{<!-- --></span><span class="token comment">/*** 静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。*/</span><span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">int</span> onlineCount <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token comment">/*** concurrent包的线程安全Map,用来存放每个客户端对应的MyWebSocket对象。*/</span><span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token class-name">ConcurrentHashMap</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">ChatWebSocketServer</span><span class="token punctuation">></span></span> chatWebSocketMap <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ConcurrentHashMap</span><span class="token generics"><span class="token punctuation"><</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">/*** 与某个客户端的连接会话,需要通过它来给客户端发送数据*/</span><span class="token keyword">private</span> <span class="token class-name">Session</span> session<span class="token punctuation">;</span><span class="token comment">/*** 接收的username*/</span><span class="token keyword">private</span> <span class="token class-name">String</span> username <span class="token operator">=</span> <span class="token string">""</span><span class="token punctuation">;</span><span class="token keyword">private</span> <span class="token class-name">UserLog</span> userLog<span class="token punctuation">;</span><span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token class-name">UserService</span> userService<span class="token punctuation">;</span><span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token class-name">UserLogService</span> userLogService<span class="token punctuation">;</span><span class="token annotation punctuation">@Resource</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setUserService</span><span class="token punctuation">(</span><span class="token class-name">UserService</span> userService<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token class-name">ChatWebSocketServer</span><span class="token punctuation">.</span>userService <span class="token operator">=</span> userService<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token annotation punctuation">@Resource</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setUserLogService</span><span class="token punctuation">(</span><span class="token class-name">UserLogService</span> userLogService<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token class-name">ChatWebSocketServer</span><span class="token punctuation">.</span>userLogService <span class="token operator">=</span> userLogService<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">private</span> <span class="token class-name">ObjectMapper</span> objectMapper <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ObjectMapper</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token class-name">ChatModel</span> chatModel<span class="token punctuation">;</span><span class="token annotation punctuation">@Resource</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setChatModel</span><span class="token punctuation">(</span><span class="token class-name">ChatModel</span> chatModel<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token class-name">ChatWebSocketServer</span><span class="token punctuation">.</span>chatModel <span class="token operator">=</span> chatModel<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token class-name">ChatRequestParameter</span> chatRequestParameter <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ChatRequestParameter</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">/*** 建立连接* @param session 会话* @param username 连接用户名称*/</span><span class="token annotation punctuation">@OnOpen</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">onOpen</span><span class="token punctuation">(</span><span class="token class-name">Session</span> session<span class="token punctuation">,</span> <span class="token annotation punctuation">@PathParam</span><span class="token punctuation">(</span><span class="token string">"username"</span><span class="token punctuation">)</span> <span class="token class-name">String</span> username<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token keyword">this</span><span class="token punctuation">.</span>session <span class="token operator">=</span> session<span class="token punctuation">;</span><span class="token keyword">this</span><span class="token punctuation">.</span>username <span class="token operator">=</span> username<span class="token punctuation">;</span><span class="token keyword">this</span><span class="token punctuation">.</span>userLog <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">UserLog</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 这里的用户id不可能为null,出现null,那么就是非法请求</span><span class="token keyword">try</span> <span class="token punctuation">{<!-- --></span><span class="token keyword">this</span><span class="token punctuation">.</span>userLog<span class="token punctuation">.</span><span class="token function">setUserId</span><span class="token punctuation">(</span>userService<span class="token punctuation">.</span><span class="token function">queryByName</span><span class="token punctuation">(</span>username<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">try</span> <span class="token punctuation">{<!-- --></span>session<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> ex<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>ex<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">this</span><span class="token punctuation">.</span>userLog<span class="token punctuation">.</span><span class="token function">setUsername</span><span class="token punctuation">(</span>username<span class="token punctuation">)</span><span class="token punctuation">;</span>chatWebSocketMap<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span>username<span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">;</span>onlineCount<span class="token operator">++</span><span class="token punctuation">;</span><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>username <span class="token operator">+</span> <span class="token string">"--open"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token annotation punctuation">@OnClose</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">onClose</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>chatWebSocketMap<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span>username<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>username <span class="token operator">+</span> <span class="token string">"--close"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token annotation punctuation">@OnMessage</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">onMessage</span><span class="token punctuation">(</span><span class="token class-name">String</span> message<span class="token punctuation">,</span> <span class="token class-name">Session</span> session<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>username <span class="token operator">+</span> <span class="token string">"--"</span> <span class="token operator">+</span> message<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 记录日志</span><span class="token keyword">this</span><span class="token punctuation">.</span>userLog<span class="token punctuation">.</span><span class="token function">setDateTime</span><span class="token punctuation">(</span><span class="token class-name">LocalDateTime</span><span class="token punctuation">.</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">this</span><span class="token punctuation">.</span>userLog<span class="token punctuation">.</span><span class="token function">setPreLogId</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>userLog<span class="token punctuation">.</span><span class="token function">getLogId</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">?</span> <span class="token operator">-</span><span class="token number">1</span> <span class="token operator">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span>userLog<span class="token punctuation">.</span><span class="token function">getLogId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">this</span><span class="token punctuation">.</span>userLog<span class="token punctuation">.</span><span class="token function">setLogId</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">this</span><span class="token punctuation">.</span>userLog<span class="token punctuation">.</span><span class="token function">setQuestion</span><span class="token punctuation">(</span>message<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">long</span> start <span class="token operator">=</span> <span class="token class-name">System</span><span class="token punctuation">.</span><span class="token function">currentTimeMillis</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 这里就会返回结果</span><span class="token class-name">String</span> answer <span class="token operator">=</span> chatModel<span class="token punctuation">.</span><span class="token function">getAnswer</span><span class="token punctuation">(</span>session<span class="token punctuation">,</span> chatRequestParameter<span class="token punctuation">,</span> message<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">long</span> end <span class="token operator">=</span> <span class="token class-name">System</span><span class="token punctuation">.</span><span class="token function">currentTimeMillis</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">this</span><span class="token punctuation">.</span>userLog<span class="token punctuation">.</span><span class="token function">setConsumeTime</span><span class="token punctuation">(</span>end <span class="token operator">-</span> start<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">this</span><span class="token punctuation">.</span>userLog<span class="token punctuation">.</span><span class="token function">setAnswer</span><span class="token punctuation">(</span>answer<span class="token punctuation">)</span><span class="token punctuation">;</span>userLogService<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span>userLog<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token annotation punctuation">@OnError</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">onError</span><span class="token punctuation">(</span><span class="token class-name">Session</span> session<span class="token punctuation">,</span> <span class="token class-name">Throwable</span> error<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>error<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">sendMessage</span><span class="token punctuation">(</span><span class="token class-name">String</span> message<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">{<!-- --></span><span class="token keyword">this</span><span class="token punctuation">.</span>session<span class="token punctuation">.</span><span class="token function">getBasicRemote</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">sendText</span><span class="token punctuation">(</span>message<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">sendInfo</span><span class="token punctuation">(</span><span class="token class-name">String</span> message<span class="token punctuation">,</span> <span class="token class-name">String</span> toUserId<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">{<!-- --></span>chatWebSocketMap<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>toUserId<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">sendMessage</span><span class="token punctuation">(</span>message<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span> <span class="token punctuation">}</span></code></pre> <blockquote> <p>我们只需要编写简单的前端代码,就可以实现和后端的socket通信。对于后端,我们只需要改一下apiKey和数据库配置就可以直接运行了。</p> </blockquote> <hr> <h1> <a id="_363"></a>微信小程序代码说明</h1> <p>我写了一个简单微信小程序来和后端进行通信,界面如下</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/ae4d41ef13f94a5495bcb9505f701fc7.png" alt="在这里插入图片描述"></p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/cc9df1be78b3485c88396e8b2f773fc5.png" alt="在这里插入图片描述"><br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/9c66f5dfcc9f4ee39e13e58c50b5742a.png" alt="在这里插入图片描述"><br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/88278e88b2a745aaa62ce368b9dd0b30.png" alt="在这里插入图片描述"></p> <p>大家只需要下载源代码,然将程序中的ip改为自己服务器的ip即可</p> <hr> <h1> <a id="_376"></a>代码链接</h1> <p>github的地址为 <strong>https://github.com/c-ttpfx/chatgpt-java-wx</strong><br> 可以直接使用 <strong>git clone https://github.com/c-ttpfx/chatgpt-java-wx.git</strong> 下载代码到本地</p> <p>我在github里面说明了安装使用的基本步骤,大家按照步骤使用即可</p> <h1> <a id="_382"></a>总结</h1> <p>上面聊天小程序就是我花2天写出来的,可能会有一些bug,我自己测试的时候倒是没有怎么遇到bug,聊天和登录功能都能正常使用。</p> <p>对于微信小程序,由于我不是专业搞前端的,就只东拼西凑实现了最基本的功能(登录、聊天),大家可以自己写一个,反正后端接口都提供好了嘛,也不是很难,不想写也可以将就使用我的。</p> <p><strong>最后,也是最重要的,大家帮我的代码star一下!!! 感谢大家了<sub>(≥▽≤)/</sub><sub>(≥▽≤)/</sub></strong></p> </div> <link href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/markdown_views-0407448025.css" rel="stylesheet"> <link href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/style-c216769e99.css" rel="stylesheet"> </div> <div id="treeSkill"></div> </article>
使用chatgpt实现微信聊天小程序(秒回复),github开源(附带链接)
作者
sockstack
许可协议
CC BY 4.0
发布于
2023-12-05
修改于
2024-11-24
上一篇:软件:常用 Linux 软件汇总,值得收藏
下一篇:ChatGPT的平替来了?一文总结 ChatGPT 的开源平替,你值得拥有
尚未登录
登录 / 注册
文章分类
博客重构之路
5
Spring Boot简单入门
4
k8s 入门教程
0
MySQL 知识
1
NSQ 消息队列
0
ThinkPHP5 源码分析
5
使用 Docker 从零开始搭建私人代码仓库
3
日常开发汇总
3
标签列表
springboot
hyperf
swoole
webman
php
多线程
数据结构
docker
k8s
thinkphp
mysql
tailwindcss
flowbite
css
前端