Websocket 在线调试工具,用于快速测试 WebSocket 服务连接。支持 ws/wss 协议,可填写地址、自定义请求头、设置子协议,建立连接后收发文本消息,实时展示收发日志。本地浏览器运行,数据不对外上传,可查看连接状态、错误信息,便于后端接口、即时通讯业务调试,快速排查握手、消息推送、断连重连等问题。
Node.js 服务端示例({{ archType == 'socketIo' ? 'Socket.IO' : '原生 WebSocket' }})▼
const WebSocket = require('ws');
// 创建WebSocket服务器实例,监听端口3000
const wss = new WebSocket.Server({ port: 3000 });
wss.on('connection', function connection(ws) {
// 当客户端连接时触发
console.log('connection')
ws.on('message', function incoming(message) {
// 当服务器接收到客户端发来的消息时触发
console.log('received:', message.toString());
ws.send(message.toString())
});
// 发送消息到客户端
ws.send('something');
});
console.log('WebSocket server is running on ws://localhost:3000');
const http = require('http');
const { Server } = require('socket.io');
// 创建HTTP服务器 + Socket.IO服务器,监听端口3000
const server = http.createServer();
const io = new Server(server, { cors: { origin: '*' } });
io.on('connection', function (socket) {
// 当客户端连接时触发
console.log('connection', socket.id);
socket.on('message', function (message) {
// 当服务器接收到客户端发来的消息时触发
console.log('received:', message);
socket.emit('message', message);
});
// 发送消息到客户端
socket.emit('message', 'something');
});
server.listen(3000, () => {
console.log('Socket.IO server is running on http://localhost:3000');
});
{{ logs }}