drashland / wocket

A WebSocket library for Deno
MIT License
104 stars 4 forks source link

Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type' #110

Closed moonformeli closed 3 years ago

moonformeli commented 3 years ago

Summary

What is your question?

// socket.ts
import { Server } from 'https://deno.land/x/sockets@v0.5.0/mod.ts';

const wss: Server = new Server();
wss.run({
  hostname: '127.0.0.1',
  port: 8081,
});

export { wss };
// index.ts
import { serve } from 'https://deno.land/std@0.83.0/http/server.ts';

import { wss } from './socket.ts';

const hostname = '127.0.0.1';

const server = serve({ hostname, port: 8080 });
console.log(`HTTP webserver running.  Access it at:  http://localhost:8080/`);

for await (const request of server) {
  let bodyContent = 'Your user-agent is:\n\n';
  bodyContent += request.headers.get('user-agent') || 'Unknown';

  request.respond({ status: 200, body: bodyContent });
}

console.log(`Server started on ws://${wss.hostname}:${wss.port}`);

Run

deno run --allow-net index.ts

Result

image

This is a very simple example app in Deno. But I faced that error when I tried to use this package. What am I supposed to do to fix this?

typescript version: 4.1.3 deno version: 1.6.0

ebebbington commented 3 years ago

try importing from wocket instead, as we changed the name, and wocket will be the module we update from now on:

import { Server } from "https://deno.land/x/wocket@v0.6.2/mod.ts"
moonformeli commented 3 years ago

@ebebbington Thanks, I don't see any errors now with importing wocket, but I don't see any console log either

wss.run({
  hostname: '127.0.0.1',
  port: 8081,
});
console.log(`Server started on ws://${wss.hostname}:${wss.port}`);
moonformeli commented 3 years ago

Oh, I solved the problem! The socket has to be opened before the web server is opened Thank you for helping me :)

moonformeli commented 3 years ago

I wonder if I can use this package in my React app

ebebbington commented 3 years ago

I’m guessing you have a ws and http server running in the same file? Would you mind sharing the contents of that file? I’m just curious if you encountered a bug

and also no problem :) in relation to using it in your react application - you ‘could’, in the sense that Wocket would be your websocket server, then you can use the WebSocket API or https://github.com/drashland/socket-client to connect to it

ebebbington commented 3 years ago

Going to close this issue then