# WebsocketDemo
**Repository Path**: HeCG95/WebsocketDemo
## Basic Information
- **Project Name**: WebsocketDemo
- **Description**: WebsocketDemo在线聊天Demo,对接了聊天机器人哟~ 欢迎来撩~
- **Primary Language**: Java
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 6
- **Forks**: 0
- **Created**: 2017-08-27
- **Last Updated**: 2024-06-06
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
WebsocketDemo在线聊天Demo,对接了聊天机器人哟~ 欢迎来撩~
(1)a.后台运行效果:

b.页面使用效果:

(2)前端使用方法:
前端使用JavaScript中:
// 创建连接
var webSocket=new WebSocket("ws://localhost:8088/WebsocketDemo/websocket/"+timestamp);
// 连接成功时触发
webSocket.onopen = function(){
webSocket.send('SocketBegin');
};
var receivedContent = "收到的消息";
// 接收服务端消息
webSocket.onmessage = function(event) {
receivedContent = event.data;
};
// 向服务端推送消息
webSocket.send(JSON.stringify({
type: 'sendToServer' // 可以随便定义,但要与服务端一致,用于在服务端区分消息类型
,data: '此处为你要发送至服务端的内容' // 这里很重要o
}));