简体   繁体   English

由于不支持的协议版本,ngx-socket-io 无法连接到服务器

[英]ngx-socket-io cannot connect to server because of unsupported protocol version

I'm building a web app with angular frontend and a NodeJs server on backend, both run on different places.我正在构建一个 web 应用程序,该应用程序带有 angular 前端和后端的 NodeJs 服务器,两者都在不同的地方运行。 I'm trying to use ngx-socket-io on angular to connect to my server but I have an error:我正在尝试在 angular 上使用 ngx-socket-io 连接到我的服务器,但出现错误:

{
"code": 5,
"message": "Unsupported protocol version"}

on the browser console.在浏览器控制台上。

This is my code:这是我的代码:

app-module.ts (Angular) app-module.ts (Angular)

 ... import { IoRequestService } from './services/ioRequest.service' import { SocketIoModule, SocketIoConfig } from 'ngx-socket-io'; const config: SocketIoConfig = { url: 'http://localhost:8080', options: {transports: [ "websocket" ], withCredentials: true} }; @NgModule({... imports: [ BrowserModule, SocketIoModule.forRoot(config), AppRoutingModule, FormsModule, GoogleChartsModule ], providers: [ IoRequestService ], bootstrap: [AppComponent] }) export class AppModule { }
Io service Io 服务
 import { Socket } from 'ngx-socket-io'; import { Injectable } from '@angular/core'; import { map } from 'rxjs/operators'; @Injectable() export class IoRequestService { constructor(private socket: Socket) { } sendMessage(msg: string) { this.socket.emit("message", msg); console.log("msg send"); } getMessage() { return this.socket.fromEvent("message").pipe(map((data:any) => data.msg)); } }

use of io service:使用 io 服务:

 ... export class TransactionsComponent implements OnInit { transactions: Transaction[] = []; constructor(private serviceIO: IoRequestService) { } ngOnInit(): void { } send() { console.log("button clicked"); this.serviceIO.sendMessage("test"); } }

And nodeJS backend:和 nodeJS 后端:

 var http = require('http'); var express = require('express'); var mysql = require('mysql'); var app = express(); var server = http.createServer(app); const io = require('socket.io')(server, { cors: { origin: "http://localhost:4200", methods: ["GET", "POST"], credentials: true, allowEIO3: true } }); var connection = mysql.createConnection({ host: 'localhost', user: 'student', password: 'std___01', database: 'elevage' }); app.engine('html', require('ejs').renderFile); app.set('view engine', 'html'); app.set('views', __dirname); app.get('/', function (req, res) { res.sendFile(__dirname + '/index.html'); }); io.on('connection', (socket) => { console.log('a user connected'); socket.on('disconnect', () => { console.log('user disconnected'); }); socket.on('message', (socket)=> { console.log('I win the game'); }) }); //handle unfound pages app.use(function(req, res, next){ res.setHeader('Content-Type', 'text/plain'); res.status(404).send('Page introuvable;'); }). server,listen(8080. (err) => { if (err) { return console,log('something bad happened'; err). } console;log(`server is on`); });

Any Idea what the problem is?任何想法是什么问题? I saw that people also use socket.io-client but ngx-socket-io seems way easier, maybe that's the issue but everything seems ok when I look at the docs.我看到人们也使用 socket.io-client 但 ngx-socket-io 似乎更容易,也许这就是问题但是当我查看文档时一切似乎都很好。

There ( https://socket.io/docs/v3/migrating-from-2-x-to-3-0/index.html ) I found that issue may be solved by setting allowEIO3: true , but it didn't worked.那里( https://socket.io/docs/v3/migrating-from-2-x-to-3-0/index.html )我发现这个问题可以通过设置allowEIO3: true来解决,但它没有工作。 Or maybe it comes from the browser?或者它可能来自浏览器? I tried on both firefox and safari latest version and I have the same issue.我尝试了 firefox 和 safari 最新版本,我有同样的问题。

When I install the npm package, I had to force it.当我安装 npm package 时,我不得不强制它。 It may encounter issue because of the repesitories structures?由于repestories结构可能会遇到问题? which look like that:看起来像这样:

  • project项目
  • angular-app角度应用
    • node_module (for Angular) node_module(用于 Angular)
  • nodejs-app nodejs-应用程序
  • node_module (for NodeJs) node_module(用于 NodeJs)

Are you requiered to use ngx-socket-io?你需要使用 ngx-socket-io 吗? I had the same issue and replaced it by socket.io-client version 3.1.2 It seems to work fine for me with this package.我遇到了同样的问题,并用socket.io-client版本 3.1.2 替换它似乎对我来说用这个 package 工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM