本文介绍了如何包括angularjs和Ex press.js socket.io?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用socket.io与和的NodeJS角

Using socket.io with nodejs and angular

myApp.factory('socket', function ($rootScope) {
  var socket = io.connect('http://localhost');

IO没有定义的错误

'io' not defined error

在EX preSS我正在写: -

In express i'm writing:-

var io = require('socket.io').listen(app.listen(app.get('port')));
io.sockets.on('connection', function (socket) {
    console.log('io.socket connection');
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

服务器没有显示错误。

The server is not showing error.

我试图按照<一个href=\"http://net.tutsplus.com/tutorials/javascript-ajax/real-time-chat-with-nodejs-socket-io-and-ex$p$pssjs/\"相对=nofollow>这个教程。

编辑: -
当我添加&LT;脚本SRC =htt​​p://cdn.socket.io/stable/socket.io.js&GT;&LT; / SCRIPT&GT;

它说连接不是对象的方法IO

it says connect is not a method of object 'io'

推荐答案

Socket.io指的是服务器和客户端上安装两个。通过调用要求()您使用服务器端库在服务器上,但你仍然需要在浏览器中的客户端库。

Socket.io is meant to be installed both on the server and on the client. By calling require() on the server you use the server-side library, but you still need to include the client-side library in the browser.

将这个地方在HTML文件(提供):

Put this somewhere in your HTML file (CDN provided by Cloudflare):

<script src="http://cdnjs.cloudflare.com/ajax/libs/socket.io/0.9.16/socket.io.min.js"></script>

请注意这是在基本的例子详细的。

Note this is detailed in the basic example on the front page of the Socket.io website.

这篇关于如何包括angularjs和Ex press.js socket.io?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 18:32
查看更多