无法在打字稿中初始化 WebSocket 结构

问题描述 投票:0回答:0

问题

当我运行以下代码时,我收到以下错误。

index.tsx

import WebSocket from 'ws';

export default function Home() {
  const socket = new WebSocket('ws://localhost:1919/ws');
  
  return (
    <div>Home</div>
  );
}

错误

Unhandled Runtime Error

Error: ws does not work in the browser. Browser clients must use the native WebSocket object

重现错误的条件

  • 初始化项目

    yarn create next-app
    

    所有选项都被选择为默认值。

  • 添加

    ws

    yarn add ws
    yarn add @types/node @types/ws
    
typescript websocket next
© www.soinside.com 2019 - 2024. All rights reserved.