mirror of https://github.com/msgbyte/tailchat
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.2 KiB
TypeScript
69 lines
1.2 KiB
TypeScript
4 years ago
|
import { io } from 'socket.io-client';
|
||
4 years ago
|
import fetch from 'node-fetch';
|
||
4 years ago
|
|
||
4 years ago
|
const uri = 'http://127.0.0.1:11000';
|
||
4 years ago
|
|
||
4 years ago
|
function createIO(authToken: string) {
|
||
|
const socket = io(uri, {
|
||
|
transports: ['websocket'],
|
||
|
forceNew: true,
|
||
|
auth: {
|
||
|
token: authToken,
|
||
|
},
|
||
|
});
|
||
4 years ago
|
|
||
4 years ago
|
// client-side
|
||
|
socket.on('connect', () => {
|
||
4 years ago
|
socket.emit(
|
||
4 years ago
|
'debug.echo',
|
||
4 years ago
|
{
|
||
|
name: 'moonrailgun',
|
||
|
},
|
||
|
(d) => {
|
||
|
console.log(d);
|
||
|
}
|
||
|
);
|
||
4 years ago
|
});
|
||
4 years ago
|
|
||
4 years ago
|
socket.on('disconnect', () => {
|
||
|
console.log(socket.id); // undefined
|
||
|
});
|
||
4 years ago
|
|
||
4 years ago
|
socket.on('connect_error', (err) => {
|
||
|
console.log('connect_error', err.message);
|
||
|
});
|
||
|
|
||
|
socket.io.on('error', () => {
|
||
|
console.log('error');
|
||
|
});
|
||
|
|
||
|
socket.onAny((eventName: string, eventData: unknown) => {
|
||
4 years ago
|
console.log({
|
||
4 years ago
|
eventName,
|
||
|
eventData,
|
||
|
});
|
||
|
});
|
||
|
}
|
||
4 years ago
|
|
||
|
fetch('http://127.0.0.1:11000/api/user/login', {
|
||
|
headers: {
|
||
|
'Content-Type': 'application/json',
|
||
|
},
|
||
|
body: JSON.stringify({
|
||
|
username: 'asd',
|
||
|
password: 'asd',
|
||
|
}),
|
||
|
method: 'post',
|
||
|
})
|
||
|
.then((res: any) => res.json())
|
||
|
.then((data: any) => {
|
||
4 years ago
|
console.log('data', data);
|
||
4 years ago
|
const token = data.user.token;
|
||
|
createIO(token);
|
||
|
});
|
||
|
|
||
|
// createIO();
|
||
|
// for (let i = 0; i < 100; i++) {
|
||
|
// createIO(i)
|
||
|
// }
|