-
-
Notifications
You must be signed in to change notification settings - Fork 233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[question] Creating a websocket server using Typescript #487
Comments
The problem is in That type mismatch with the one on However you can also use another way: const { createWebSocketStream, Server } = require('ws')
function createWebsocketServer (server, broker) {
const ws = new Server({ server })
ws.on('connection', function (conn, req) {
var stream = createWebSocketStream(conn)
broker.handle(stream)
})
}
const http = require('http')
const server = http.createServer()
// Than use this function to bind aedes to your server
createWebsocketServer(server, broker) |
Sorry, looks like I +1 too quick!
throws an error, and it looks like broker.handle only accepts one param. BTW, if we can get this sorted out, I'd be happy to open a PR for some better Typescript examples. |
@briant-spindance Try with my updated comment now and let me know if it works |
@robertsLando That worked! Thanks for the help. |
Sorry, I have a similar problem, I'm trying to make a project in typescript with MQTT, express and websockets functionality but I don't know how to implement the solution mentioned in the previous comment import express from "express";
import { Server, Client, AuthenticateError } from "aedes";
import { createServer, Socket } from "net";
import * as http from "http";
//import * as ws from "websocket-stream" //problem "Argument of type '(stream: Connection) => Client' is not assignable to parameter of type '() => void'."
var ws = require("websocket-stream");
const broker = Server({
heartbeatInterval: 60000,
});
broker.on("publish", (packet, client) => {
if (client) {
console.log("%s : topic %s : %s", client.id, packet.topic, packet.payload);
}
});
// Initializations
const app = express();
// Settings
app.set("port", process.env.PORT || 3000);
//ws server
/* Problem createWebSocketStream is not a function
import { createWebSocketStream, Server } from 'ws';
const serverws = http.createServer();
const ws = new Server({server:serverws, port:2000})
ws.on('connection', function (conn, req) {
var stream = createWebSocketStream(conn)
broker.handle(stream)
})
*/
var wsServer = http.createServer();
ws.createServer({ server: wsServer }, broker.handle);
wsServer.listen(2000, function () {
console.log("functional WS");
});
// MQTT server
const server = createServer(broker.handle);
server.listen(1883);
//test Express
app.get("/", function (req, res) {
res.send("Hello Worlds!");
});
//Express server
app.listen(app.get("port"), () => {
console.log(`server on port ${app.get("port")}`);
}); ` |
|
Yes, thank you, it was a bit frustrating, I installed WS and it didn't fail to find the module, but when I reinstalled it, it was fixed Thank you very much, I'll keep working |
I'm trying to make a create a websocket server in Typescript, and I can't seem to translate the Javascript examples I've found into Typescript.
Here's what I have so far:
I'm getting an error:
"Argument of type '(stream: Connection) => Client' is not assignable to parameter of type '() => void'."
Any pointers on where I'm going wrong?
The text was updated successfully, but these errors were encountered: