如何把tcp socket 的 定位器终端 经过 node 在 http web 界面里进行指令的收发通讯,找到了一个项目,估计要进行一定的修改,因为 tcp socket 无法在 http web 界面里进行通讯, https://github.com/uditalias/chat-nodejs
5 回复
try inject socket.io into your web server and you will get the ability to real time control your tcp behaviors /
已经通过 net socket.io 两个模块 实现了 浏览器 到 tcp socket server 的数据发送, 详见:http://stackoverflow.com/questions/11967958/create-websockets-between-a-tcp-server-and-http-server-in-node-js
现有个简单问题:如下 index.html 问题,访问后会 发送 “For TCP” 到TCP socket,
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost');
socket.on('httpServer', function (data) {
console.log(data);
document.write(data + "\r\n");
socket.emit('tcp', "For TCP");
});
</script>
但如何做成表单发送?send.html
<script language="javascript">
function getSelectValue(_myselect){
var oForm = document.forms["myForm1"];
//根据参数(下拉列表框的name属性值)获得下拉菜单项
var oSelectBox = oForm.elements[_myselect];
//判断是单选还是多选
if(oSelectBox.type == "select-one"){
var iChoice = oSelectBox.selectedIndex; //获取选中项
alert("单选,您选中了" + oSelectBox.options[iChoice].text);
}
else{
var aChoices = new Array();
//遍历整个下拉菜单
for(var i=0;i<oSelectBox.options.length;i++)
if(oSelectBox.options[i].selected)//如果被选中
//压入到数组中
aChoices.push(oSelectBox.options[i].text);
//输出结果
alert("多选,您选了:" + aChoices.join());
}
}
</script>
<form method="post" name="myForm1">
<select id="mysel" name="mysel" multiple="multiple" style="height:60px;">
<option value="a">AA</option>
<option value="b">BB</option>
<option value="c">CC</option>
</select>
<input type="button" onclick="getSelectValue('mysel')" value="查看选项" />
解决了
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
$(function(){
var iosocket = io.connect('http://www.youserver.com');
$(document).ready(function(){
$("button").click(function(){
iosocket.emit('tcp', $("#test").val());
// iosocket.emit('tcp', $("#test1").val());
});
});
});
</script>
</head>
<body>
<p>COMMAND:<input type="text" id="test" value="thedatafromwebpage#"></p>
<p>COMMAND:<input type="text" id="test1" value="*HQAAAA"></p>
<button>send</button>
</body>
</html>