mirror of
https://github.com/alwxkxk/soft-and-hard.git
synced 2025-01-14 06:12:52 +08:00
fix: 修复demo0.1显示客户端错误地址。
This commit is contained in:
parent
064f0ef5c6
commit
d557d0aef6
@ -49,8 +49,8 @@
|
||||
## V2.0 升级内容
|
||||
根据过去几年来,解决各位同学的问题后,为提升学习体验,于2021/7/17着手开始迭代升级为V2.0:
|
||||
- demo1 也使用HTTP轮询。
|
||||
- 不再使用pug模板,减少不必要的学习内容。
|
||||
- TCP服务器连接超时时间从10秒改成60秒,避免时间过短导致调试不方便。
|
||||
- 不再使用Jquery、pug模板,减少不必要的学习内容。
|
||||
- TCP服务器连接超时时间从10秒改成30秒,避免时间过短导致调试不方便。
|
||||
- 把原本引用CDN的外部资源全部下载放到内部引用。
|
||||
- 将教程从多条并行选择学习改变成有顺序地学习,避免读者迷惑不知所措。
|
||||
- 以前的演示都是无声的小视频,准备录制完整的有声视频。
|
||||
|
@ -1,13 +1,13 @@
|
||||
const fs = require('fs');
|
||||
let http = require('http');
|
||||
const net = require('net');
|
||||
const HTTP_PORT = "8000";
|
||||
const TCP_PORT = "9000"
|
||||
const TIMEOUT = 60000;//tcp客户端超过60秒没发数据判为超时并断开连接
|
||||
let tcpClient=null;//tcp客户端
|
||||
var fs = require('fs');
|
||||
var http = require('http');
|
||||
var net = require('net');
|
||||
var HTTP_PORT = "8000";
|
||||
var TCP_PORT = "9000"
|
||||
var TIMEOUT = 30*1000;//tcp客户端超过30秒没发数据判为超时并断开连接
|
||||
var tcpClient=null;//tcp客户端
|
||||
|
||||
// 创建http server,并传入回调函数:
|
||||
const httpServer = http.createServer(function (request, response) {
|
||||
var httpServer = http.createServer(function (request, response) {
|
||||
// 回调函数接收request和response对象,
|
||||
// 获得HTTP请求的method和url:
|
||||
console.log(request.method + ': ' + request.url);
|
||||
@ -29,15 +29,15 @@ const httpServer = http.createServer(function (request, response) {
|
||||
break;
|
||||
case "/data":
|
||||
// 获取数据
|
||||
let data = getData() || "无数据";
|
||||
var data = getData() || "无数据";
|
||||
|
||||
let addr = "无连接";
|
||||
var addr = "无连接";
|
||||
if(tcpClient && tcpClient.addr){
|
||||
addr = tcpClient.addr
|
||||
}
|
||||
|
||||
// 将结果转换成字符串再发出去
|
||||
let result = JSON.stringify({addr:addr,data:data});
|
||||
var result = JSON.stringify({addr:addr,data:data});
|
||||
response.end(result);
|
||||
break;
|
||||
default:
|
||||
@ -52,16 +52,16 @@ httpServer.on('error', onError);
|
||||
httpServer.on('listening', onListening);
|
||||
|
||||
//创建TCP服务器
|
||||
const tcpServer = net.createServer((socket)=>{
|
||||
var tcpServer = net.createServer((socket)=>{
|
||||
//connect
|
||||
let addr = socket.address().address + ':' + socket.address().port
|
||||
console.log(addr," connect.")
|
||||
var addr = socket.remoteAddress + ':' + socket.remotePort
|
||||
console.log(addr," connect.",socket)
|
||||
socket.addr = addr
|
||||
tcpClient = socket
|
||||
|
||||
// recieve data
|
||||
socket.on("data",data=>{
|
||||
let str = addr+" --> " + data.toString('ascii') + '\n'
|
||||
var str = addr+" --> " + data.toString('ascii') + '\n'
|
||||
console.log(str)
|
||||
socket.lastValue = data.toString('ascii')
|
||||
})
|
||||
|
@ -1,14 +1,3 @@
|
||||
/**
|
||||
* 文件:base/tcp1/tcp1.js
|
||||
* 作者:alwxkxk
|
||||
* 修改时间:2018/10/07
|
||||
* 描述:
|
||||
* 只是单纯地开启TCP服务器 9001端口:
|
||||
* 建立连接时返回:`${client addr} connected.\n`
|
||||
* 接收数据返回:`${client addr} -->${data}`
|
||||
* 整个教程在不断迭代更新中:https://github.com/alwxkxk/soft-and-hard
|
||||
*/
|
||||
|
||||
const net = require('net')
|
||||
const PORT = "9001"
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user