客户端代码:
Html代码
<html> <head> <title></title> <script src="../js/socket.io.client.js"></script> <script type="text/javascript"> function doit() { var socket = io.connect('http://localhost'); socket.on('news', function (data) {//接收到服务器发送过来的名为'new'的数据 console.log(data.hello);//data为应服务器发送过来的数据。 socket.emit('my new event', { my:'new data' });//向服务器发送数据,实现双向数据传输 }); socket.on('other', function (data) {//接收另一个名为'other'数据, console.log(data.hello); socket.emit('event1', { my:'other data' }); }); } </script> </head> <body> <button id='btn' onclick="doit()">click me</button> </body> </html>
在chrome,Crtl+Shift+j打开终端,可看到输出结果。
socket.io.client.js可以https://github.com/LearnBoost/socket.io-client下载到本地,在<script src="..">指向本机的js库。
服务器用nodejs实现
代码
Javascript代码
var http= require('http'), io= require('socket.io'), express= require('express'); var app = express.createServer(), io = io.listen(app); app.listen(80); io.sockets.on('connection', function (socket) { socket.emit('news', { hello: 'world' });//监听,一旦客户端连接上,即发送数据,第一个参数'new'为数据名,第二个参数既为数据 socket.on('my other event', function (data) {//捕获客户端发送名为'my other event'的数据 console.log(data.my); }); socket.emit('other', { hello: 'other world' });//发送另一个数据 socket.on('evnet1', function (data) {//捕获另外一个数据 console.log(data.my); }); });
测试结果,客户端可正常显示
引用
world
other world
other world
服务器端显示结果:
引用
C:\java\Nodejs>node server2.js
info - socket.io started
debug - client authorized
info - handshake authorized 15551970622100878177
debug - setting request GET /socket.io/1/websocket/15551970622100878177
debug - set heartbeat interval for client 15551970622100878177
debug - client authorized for
debug - websocket writing 1::
debug - websocket writing 5:::{"name":"news","args":[{"hello":"world"}]}
debug - websocket writing 5:::{"name":"other","args":[{"hello":"other world"}
]}
debug - emitting heartbeat for client 15551970622100878177
debug - websocket writing 2::
debug - set heartbeat timeout for client 15551970622100878177
debug - got heartbeat packet
debug - cleared heartbeat timeout for client 15551970622100878177
debug - set heartbeat interval for client 15551970622100878177
^C
info - socket.io started
debug - client authorized
info - handshake authorized 15551970622100878177
debug - setting request GET /socket.io/1/websocket/15551970622100878177
debug - set heartbeat interval for client 15551970622100878177
debug - client authorized for
debug - websocket writing 1::
debug - websocket writing 5:::{"name":"news","args":[{"hello":"world"}]}
debug - websocket writing 5:::{"name":"other","args":[{"hello":"other world"}
]}
debug - emitting heartbeat for client 15551970622100878177
debug - websocket writing 2::
debug - set heartbeat timeout for client 15551970622100878177
debug - got heartbeat packet
debug - cleared heartbeat timeout for client 15551970622100878177
debug - set heartbeat interval for client 15551970622100878177
^C
在世界的中心呼喚愛 写道
提示找不到 express
npm install express
另外,有些IO API已经deprecated,改成:
- var http= require('http'), io= require('socket.io');
- var app = http.createServer(), io = io.listen(app);
- app.listen(81);
这样就可以不必引用express
相关推荐
socket.io-cookie 用于socket.io的Cookie解析器中间件 例子 var cookieParser = require ( 'socket.io-cookie' ) ; var server = require ( 'http' ) . Server ( ) ; var io = require ( 'socket.io' ) ( server )...
Socket.IO是一个基于WebSocket的实时通信库,在主流平台都有很好的支持,此文主要是通过一个小例子来演示Socket.IO的使用。 基础环境搭建 新建一个文件夹(JS工程),创建一个package.json,复制以下内容并保存。 ...
redux.io 用于Socket.io的Redux / React绑定 这仍然是Alpha :red_exclamation_mark: 请不要在生产中使用。... 唯一的要求是将socket.io传递给redux.io reducer作为第一个参数。 import { createStore ,
关于在vue中使用websocket的简易例子 使用vue-cli生成一个vue模版 安装三个依赖: npm install -s socket.io npm install -s vue-socket.io npm install -s socket.io-client 创建一个新的servers:在文件最外层...
它适用于浏览器上的在线实时应用程序,例如聊天,而浏览器的兼容性是一个重要问题。 两个客户端中的api几乎相同,除了websocket客户端需要协议数据的握手回调。 两个客户都使用包管理器进行构建。 ##用法 连接...
其中一个演示使用Google Maps API。 对于演示,工作,你需要申请从谷歌谷歌地图API密钥,如所描述。 要启动服务器,请运行: (venv) $ export GOOGLE_MAPS_KEY= (venv) $ export FLASK_APP=socketio_examples.py ...
实时记录器使用 node.js、socket.io 和 angular 构建的 websocket 上的语言不可知应用程序记录器安装git clone ...例子##PHP 将 ElephantIO
环回模块,用于创建Socket.io连接并允许调用方法。 安装 npm install loopback-socket --save 用法 服务器端 包括 const LoopbackSocket = require ( 'loopback-socket' ) ; LoopbackSocket.get(名称,[超时]) ...
使用 node.js 和 socket.io 制作的轻量级实时聊天应用程序您可以创建和加入的公共和私人聊天室邀请链接!格式为简单的用户界面用于自定义您的体验的聊天命令: 名称描述例子!帮助查看命令!帮助!set用户名设置...
当然读者也可以使用socket.io,专为webSocket设计的js语言的服务端,用起来非常简单,它也对不支持webSocket的浏览器提供了兼容(flash或comet实现)。 笔者本人比较喜欢使用tornado,做了几年后台开发,使用最多的...
nodeJS做的3D的多人在线游戏,用的Three.js这个开源库,包括模型的解析等,模型也是Three.js的例子上提供的,后台用的nodeJS,静态文件服务器跟websocket服务器没写在一起,websocket是用的nodeJS的socket.io模块,...
websocket远程控制客户端Websockets-remote-control是一个JS项目,提供两个库:客户端和服务器,可以通过websockets链接两个设备。 客户端(此库)该库初始化Websockets客户端。 在这里,使用screen和controller功能...
一个很好的例子,说明如何在节点集群和 redis 中正确使用 socket.io,以及如何远程控制嵌入式项目。 移动项目的 ip 不断变化,最好有一些家庭基地来打电话。 示例服务器托管在 要求 您需要使用 redis 和 socket.io ...
简单的例子服务器(Node.js) // server.js (node.js)import Server from 'angelia.io/server' ;class Connection {async listen ( ) {console . log ( '-' . repeat ( 60 ) ) ;console . log ( 'Server started ...
有关详细信息,您可以检查项目报告.pdf例子图书馆Vue.js :Vue-Socket.io: cryptolong :FileSaver.js : flask : Flask-SocketIO : pycryptodome :部署方式前端您可以使用src / global.js中的变量global.base...
简单聊天软件CS模式 2个目标文件 一个简单的CS模式的聊天软件,用socket实现,比较简单。 凯撒加密解密程序 1个目标文件 1、程序结构化,用函数分别实现 2、对文件的加密,解密输出到文件 利用随机函数抽取幸运数字 ...
一个简单的CS模式的聊天软件,用socket实现,比较简单。 凯撒加密解密程序 1个目标文件 1、程序结构化,用函数分别实现 2、对文件的加密,解密输出到文件 利用随机函数抽取幸运数字 简单 EJB的真实世界模型(源代码...
八个球 使用和在中制作的多人8球池游戏。 安装 克隆git仓库: $ git clone https://github.com/aakashkcx/eight-ball.git 使用npm安装依赖项: $ npm install 启动服务器: $ npm start 该服务器将在上运行。 ...