Node.js socket.io前端后端通讯简单例子
来源:互联网 发布:python get threshold 编辑:程序博客网 时间:2024/06/05 11:40
前端代码:
<!doctype html><html><head> <style> * { margin: 0; padding: 0; } html, body { width: 100%; height: 100%; } #box { position: absolute; bottom: 0; } </style></head><body> <ul id="message-box"></ul> <div id="box"> <input id="edit-box" autocomplete="off" /><button id="btn">Send</button> </div> <script src="socket.io.js"></script> <script> var socket = io(); document.getElementById('btn').onclick = function () { socket.emit('message', document.getElementById('edit-box').value); document.getElementById('edit-box').value = ''; }; socket.on('message', function (message) { var li = document.createElement('li'); li.innerHTML = message; document.getElementById('message-box').appendChild(li); }); </script></body></html>
后端代码:
var express = require('express');var app = express();var http = require('http').Server(app);var io = require('socket.io')(http);var path = require('path');app.use(express.static(__dirname));app.get('/', function (request, response) { response.sendFile(path.join(__dirname, 'index.html'));});io.on('connection', function (socket) { console.log('a user connected'); socket.on('disconnect', function () { console.log('user disconnected'); }); socket.on('message', function (message) { console.log('message: ' + message); io.emit('message', message); });});var server = http.listen(4000, function () { console.log('Sever is running');});
阅读全文
0 0
- Node.js socket.io前端后端通讯简单例子
- Node.js+socket.io简单在线聊天
- socket通讯简单例子
- node.js服务端和QT客户端简单Socket通讯
- 基于Node.js,Express,Socket.io创建简单聊天室
- socket.io node.js mysql
- 实现基于Node.js的ajax前后端交互的简单例子
- [Linux] 最简单的socket通讯例子
- 一个最简单的socket通讯例子
- ubuntu install node.js socket.io
- Socket.IO 和 Node.js 入门
- node.js+socket.io配置详解
- node.js+socket.io+聊天室源码
- Socket.IO 和 Node.js 聊天程序
- node.js+socket.io私人聊天
- Socket.IO 和 Node.js 入门
- Socket.IO 和 Node.js 聊天程序
- Node.js+socket.io在线聊天室
- 牛客1(华为)
- 数据库连接池(4)-项目中的使用
- 链表-leetcode 82 Remove Duplicates from Sorted List II
- [C++ Primer Plus]学习笔记--关于C++ string和c类型字符数组的对比
- linux --- 杀掉特定端口进程与启用SSH服务
- Node.js socket.io前端后端通讯简单例子
- 插入排序
- urllib 和 request 对比
- 设计模式之单例模式
- samba服务器的配置与使用
- tls
- Hi,我们再来聊一聊Java的单例吧
- Cgroups控制cpu,内存,io示例
- Maven的聚合与继承