Socket.io-client: 库socket.io-client与最新的Angular 6不兼容

创建于 2018-05-13  ·  16评论  ·  资料来源: socketio/socket.io-client

我想要:

  • [x]报告错误-已解决,请查找最后两行以解决

库socket.io-client与最新的Angular 6不兼容。执行时出错:

ReferenceError:全局未在Object ../ node_modules / socket.io-client / node_modules / socket.io-parser / is-buffer.js中定义(is-buffer.js:4)

请阅读下面的评论以了解更多信息:
https://github.com/angular/angular-cli/issues/9827#issuecomment -369578814

如果您想亲自查看此错误,请添加到以下所示的angular 6.0应用程序简单服务中:

import { Injectable } from 'angular/core';
import * as socketIo from 'socket.io-client';

@Injectable()
export class SocketService {
    private socket;

    constructor() {}

    public initSocket(): void {
        this.socket = socketIo();
    }
}

删除initSocket函数时错误消失。



好的,Angular 6中有一种解决方法可以与socket.io-client一起使用。
添加(window as any).global = window; 到polyfills.ts

最有用的评论

我的工作开始时只需添加以下内容

polyfills.ts添加:

(window as any).global = window;

角6.1.0
socket.io-client 2.1.1

升级到Angular 7.1.x后,此操作继续有效

所有16条评论

在项目及其依赖项中确实存在许多global引用: https :

我不确定删除这些引用的副作用。

@darrachequesne是否可以将这些更改添加到项目的分支中,直到正确解决?

@jessycormier “项目的分支”是什么意思?

第一个副作用是,似乎删除https://github.com/darrachequesne/has-binary/pull/4中的全局https :

你好这有什么进展吗? 有什么办法吗? 任何建议的解决方法?

@realshaft-以上变通办法在Ionic 4 / Angular 6 Web应用程序中对我

好的,Angular 6中有一种解决方法可以与socket.io-client一起使用。
(window as any).global = window;添加到polyfills.ts (位于src文件夹中)

@realshaft谢谢你,它可以在angular6中工作;)

你好@所有人
请帮帮我。
所以我创建了一个服务来处理我的websocket连接并将其注入一个组件。
当我在服务器控制台中获得 a user connected ,与套接字的连接很好。
但是当我尝试从组件发送消息时,什么也没发送。
这是我的文件:
socketService.ts

从“ @ angular / core”导入{可注射};
从“ rxjs / Observable”导入{Observable};
从“ rxjs / Subject”导入{Subject};
从'socket.io-client'导入*作为io;

 @Injectable({
 providerIn:'root'
 })
导出类SocketService {

 私人网址='http:// localhost:3000';
 专用套接字= io(this.url);

 //向套接字发送消息
 讯息(讯息){
 //发送信息
 this.socket.emit('chat',msg);
 }

 chat(msg){
 this.socket $ .emit('chat',msg);

 //让我们观察
 }
 }

和组件文件

从'@ angular / core'导入{Component,OnInit};
从“ ../../socket.service”导入{SocketService};

 @零件({
 选择器:“ app-socket”,
 templateUrl:“ ./ socket.component.html”,
 styleUrls:['./ socket.component.scss']
 })
导出类SocketComponent实现OnInit {

 构造函数
 专用套接字:SocketService
 ){}

 ngOnInit(){
 }

 send(){
 const msg ='hello';
 this.socket.message(msg);
 }

 }

这是我的套接字服务器连接。 我用了MVC

 const gameModel = require('../ models / game')
 const userModel = require('../ models / users')
 const wsModel = require('../ models / socket')
 const socket = require('socket.io')

 module.exports =(服务器)=> {

 const io = socket(服务器);

 // WebSocket处理程序
 io.on('connection',(socket)=> {
 console.log('用户已连接')
 })

 //获取聊天消息
 io.on('chat',(msg)=> {
 // io.emit('res',json.stringify(msg))
 console.log(msg);
 })

 }

先感谢您

这是一种有点怪异的方法来使其工作:
从CDN下载socket.io-client.jssrc文件夹中

main.ts添加以下行:

import * as io_ from './socket.io-client.js';
window['io'] = io_;

polyfills.ts添加:

(window as any).global = window;

现在,您可以通过以下方式使用呼叫socket.io-client

window['io']('localhost')

我的工作开始时只需添加以下内容

polyfills.ts添加:

(window as any).global = window;

角6.1.0
socket.io-client 2.1.1

升级到Angular 7.1.x后,此操作继续有效

它可以帮助我,但是为什么呢?

@realshaft谢谢你,它可以在angular6中工作;)

谢谢,它在Angular 7.x上也对我有用。

(window as any).global = window;
这在Angular 7上也对我有效。

同样的解决方案适用于角度8

上帝不敢相信我这么说。 提供了相同的解决方案@mjarkk 。 可在angular10中使用! -对我来说,原因是2.x服务器无法与3.x客户端一起使用!

在3个版本之后,此hack stil可以运行lmao,我敢打赌,它在角度11中的运行方式相同。
但是再次看到此问题弹出窗口时,我现在建议您使用本机javascript WebSocket,因为所有主要浏览器均支持它们,以供入门教程使用: https :

此页面是否有帮助?
0 / 5 - 0 等级