我正在使用node.js
并希望收听我在server.ts中打开的websocket:
import * as express from 'express';
import * as http from 'http';
import * as WebSocket from 'ws';
const app = express();
const server = http.createServer();
const ws_server = new WebSocket.Server({ server });
/**
@Instructions:
1. on console run: ts-node server.ts
2. Navigate to chrome-extension://pfdhoblngboilpfeibdedpjgfnlcodoo/index.html and set URL to
*/
ws_server.on('connection', (ws: WebSocket) => {
// connection is up, add simple event:
ws.on('message', (message: string) => {
console.log('received: ' + message);
ws.send(`hello, you sent -> ${message}`);
});
// send feedback to incoming connection
ws.send('Hi there, I am a websocket server from server.ts');
});
server.listen(8999, () => {
console.log(`Server started on port ${server.address().port}`);
});
现在在app.ts
我有这个:
import * as express from 'express';
import * as http from 'http' ;
import * as WebSocket from 'ws' ;
import * as net from 'net' ;
// declare instance of web application
const app = express();
// home page
app.get('/', (req, res) =>
res.send(`hello world from app.ts with rand num: ${Math.floor(Math.random()*100)}`)
);
// .... I would like to listen to port 8999 and log the information to console, as well as
// upate the front end in real time
// start the app
app.listen(3000, () => console.log("web app listening on port 3000"));
用于侦听端口8999
的库是什么?