将socket.io与aurelia一起使用
我正在尝试在框架aurelia中使用socket.io。但当我启动服务器时将socket.io与aurelia一起使用,socket.io,aurelia,Socket.io,Aurelia,我正在尝试在框架aurelia中使用socket.io。但当我启动服务器时 error /Users/pierrehedkvist/TDDD272017/picasso/node_modules/socket.io-client/dist/socket.io.min.js 我正在将socket.io添加到我的aurelia.json文件中,如下所示: "dependencies": [ .... { "name": "socket.io-cl
error /Users/pierrehedkvist/TDDD272017/picasso/node_modules/socket.io-client/dist/socket.io.min.js
我正在将socket.io添加到我的aurelia.json文件中,如下所示:
"dependencies": [
....
{
"name": "socket.io-client",
"path": "../node_modules/socket.io-client/dist/socket.io.min"
}
]
我像这样使用它(app.js)。我尝试导入socket.io-client并测试是否可以写入服务器
import {inject} from 'aurelia-framework';
import {ApplicationState} from './application-state';
import io from 'socket.io-client';
var socket = io.connect( 'http://localhost:3000' );
io.emit('chat message', "TESTING");
@inject(ApplicationState)
export class App {
constructor (appState) {
this.appState = appState;
console.log(this.appState);
//this.appState.test = "Billy";
this.players = "";
}
/*activate() {
socket.on('chat message', function(msg){
io.emit('chat message', "TESTING");
console.log('message: ' + msg);
});
}*/
configureRouter(config, router) {
config.title = 'Piccasso or Not';
config.map([
{ route: '', moduleId: 'home', nav:true, name: "Home", title: 'Home'},
{ route: 'game', name: 'game',
moduleId: 'control/game', nav: true, title:'Create a game' },
{ route: 'draw', name: 'draw',
moduleId: 'control/draw', nav: true, title:'Draw something' },
{ route: 'theme', name: 'theme',
moduleId: 'theme', nav: true, title:'Theme' },
{ route: 'next-player', name: 'next-player',
moduleId: 'control/next-player', nav: true, title:'Next player' },
{ route: 'guess', name: 'guess',
moduleId: 'control/guess', nav:true, title: 'Guess'}
]);
this.router = router;
}
activate() {
this.message = 'Hellow world';
}
joinGame() {
console.log("Join lobby " + this.lobbyID)
}
createGame() {
console.log("createGame")
}
}
编辑:
所以我发现我的路径不正确,现在是这样
"name": "socket.io-client",
"path": "../node_modules/socket.io-client/socket.io.min"
然后在app.js中,我将:
import io from 'socket.io-client';
var socket = io.connect( 'http://localhost:3000' );
//io.emit('chat message', "TESTING");
并创建了一个名为test()的函数,该函数通过一个按钮调用,该按钮成功地将消息发送到我的服务器
test() {
socket.emit('chat message', "testing");
}
您是否可以将所有代码复制到正在从“socket.io客户端”导入io的文件中??刚刚进行了编辑如果移动
var socket=io.connect('http://localhost:3000' ); io.emit(“聊天信息”,“测试”)代码>进入你的constructor()
方法?它在构造函数中不起作用,但我设法创建了一个可以向套接字发送消息的函数。请参阅我的编辑,thx:)您是否可以将所有代码复制到您正在执行从“socket.io客户端”导入io的文件中
?刚刚进行了编辑如果移动var socket=io.connect('http://localhost:3000' ); io.emit(“聊天信息”,“测试”)代码>进入你的constructor()
方法?它在构造函数中不起作用,但我设法创建了一个可以向套接字发送消息的函数。请参阅我的编辑,thx:)