-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.mjs
41 lines (32 loc) · 1002 Bytes
/
example.mjs
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
import OpenAI from "openai";
const openai = new OpenAI();
const completion = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{
role: "user",
content: "Write a haiku about recursion in programming.",
},
],
});
console.log(completion.choices[0].message);
// JavaScript source code
import WebSocket from "ws";
// Create WebSocket server on port 8080
const wss = new WebSocket.Server({ port: 8080 });
let clients = [];
wss.on('connection', (ws) => {
console.log('Client connected');
clients.push(ws);
// Receive messages from the client
ws.on('message', (message) => {
console.log(`Received: ${message}`);
clients[0].send(message);
});
// Client disconnected
ws.on('close', () => {
console.log('Client disconnected');
});
});
console.log('WebSocket server is running on ws://localhost:8080');