-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.ts
57 lines (47 loc) · 1.51 KB
/
client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import express from 'express';
import cors from 'cors';
import dotenv from 'dotenv';
import { io } from "socket.io-client";
import ClipboardData from './models/clipboard.model';
import { uuid } from 'uuidv4';
import clipboardy from 'clipboardy';
const app = express()
const ClipboardListener = require('clipboard-listener');
dotenv.config()
const port = process.env.PORT || 3001
const serverUrl = process.env.SERVER_BASE_URL || "http://localhost:3000"
const socket = io(serverUrl);
let clipboardData: any;
const clipboardListener = new ClipboardListener({
timeInterval: 100, // Default to 250
immediate: true, // Default is false
});
const startListening = () => {
console.log("Started Listening to Clipboard");
clipboardListener.on('change', (value: any) => {
clipboardData = value;
console.log(value);
let clipboard: ClipboardData = {
id: uuid(),
clipboard: value
};
socket.emit("clipboard-send", clipboard);
});
}
const initSocket = () => {
socket.on("connection", (...args) => {
console.log("Connected to Server");
});
socket.on("clipboard-receive", (data: ClipboardData) => {
console.log(`Data received ${data.clipboard} from socket ${socket}`);
clipboardy.writeSync(data.clipboard);
});
socket.on("disconnect", () => {
console.log("Disconnected from server");
});
}
app.listen(port, () => {
console.log(`Connected and Listening on Port : ${port}`)
startListening()
initSocket()
});