socket.io服务器与客户端之间无通信 [英] socket.io no communication between server and client
问题描述
我目前正在尝试让angular2前端与使用socket.io的node.js后端进行通信. 关键是,我将客户端连接到服务器,但是此后,无法在它们之间成功传递套接字调用. 这是服务器的简单代码:
I'm currently trying to have an angular2 frontend communicating with a node.js backend with socket.io. The point is, I get the client connected to the server, but after that, no socket call can be successfully passed between them. Here is a simple piece a code for the server :
var app = require('express')();
var http = require('http');
var server = http.createServer(app);
var io = require('socket.io').listen(server);
io.on('connection', function() {
io.emit('update');
console.log('Connected');
});
io.on('updated', function() {
io.emit('update');
console.log('Updated');
});
server.listen(5000, function() {
console.log('Listening on 5000');
});
...以及组件:
import { Component } from '@angular/core';
import * as io from 'socket.io-client';
@Component({
selector: 'main-app',
template: `
<div>
<button (click)="foo()"
style='padding:20px; background:red; color:white'>
click me
</button>
</div>
`
})
export class AppComponent {
title = 'bar';
socket = null;
constructor() {
let self = this;
self.socket = io.connect('http://mysuperwebsite:5000', {
transports : ["websocket"]
});
self.socket.on('update', function(data) {
console.log(data);
});
}
foo() {
let self = this;
self.socket.emit('updated', {});
}
}
我听不清是怎么回事,我想你会的;) 感谢您的帮助!
I can't get what is wrong, I guess you will ;) Thanks for your help !
最后,问题似乎是由于io.emit()中缺少第二个参数引起的.现在可以了,非常感谢您:)
EDIT : Finally, the problem seemed to come from the lack of second parameter in io.emit(). Now it works, thanks you very much :)
推荐答案
代替调试代码,我将向您发布一个有效的示例,您可以从那里进行操作:
Instead of debugging your code, I'll post you an example that works and you can go from there:
使用Express.js的Socket.IO服务器示例:
Socket.IO server example using Express.js:
var path = require('path');
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', (req, res) => {
console.error('express connection');
res.sendFile(path.join(__dirname, 'si.html'));
});
io.on('connection', s => {
console.error('socket.io connection');
for (var t = 0; t < 3; t++)
setTimeout(() => s.emit('message', 'message from server'), 1000*t);
});
http.listen(3002, () => console.error('listening on http://localhost:3002/'));
console.error('socket.io example');
来源: https://github.com. com/rsp/node-websocket-vs-socket.io/blob/master/si.js
使用香草JavaScript的Socket.IO客户端示例:
Socket.IO client example using vanilla JavaScript:
var l = document.getElementById('l');
var log = function (m) {
var i = document.createElement('li');
i.innerText = new Date().toISOString()+' '+m;
l.appendChild(i);
}
log('opening socket.io connection');
var s = io();
s.on('connect_error', function (m) { log("error"); });
s.on('connect', function (m) { log("socket.io connection open"); });
s.on('message', function (m) { log(m); });
来源: https://github. com/rsp/node-websocket-vs-socket.io/blob/master/si.html
可以从npm安装 或从GitHub下载.它非常简单,而且众所周知,它可以工作,因此您可以有一个工作的后端部分来测试您的前端.
That example can be installed from npm or downloaded from GitHub. It's as simple as it gets and it's known to work so you can have a working backend part to test your frontend with.
它是为此答案编写的-您可以在此处找到更多信息.
It was written for this answer - you can find mush more info there.
这篇关于socket.io服务器与客户端之间无通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!