我是socket.io的新手。我想使用它开发一个小型应用程序。我知道socket.io的基本知识。我遵循了this和this文档。
我也可以进行这种简单的连接。代码:
客户:
<script src="/socket.io/socket.io.js"></script>
<script>
console.log(location.protocol+'//'+location.host);
var socket = io.connect(location.protocol+'//'+location.host), socketwithApi = io.connect(location.protocol+'//'+location.host + '/apikey/');
</script>
<script type="text/javascript">
var i=0;
$(document).on('click','#socket-emit-btn', function(){
let message = location.href;
socket.emit("test",message);
});
socket.on("testlistened",function(data){
i ++;
console.log(i);
console.log(data);
if(data){
$("#console-div").html(data);
}else{
$("#console-div").html("{empty}");
}
});
socketwithApi.on('apikey',function(data){
console.log(data);
});
</script>
服务器:
//consider everything needed is defined
var apikey = io
.of('/apikey/')
.on('connection', function (socket) {
socket.emit('apikey', { news: 'item' });
});
io.on('connection', function (socket) {
let clientID = socket.id
let clientIP = socket.handshake.address
if (client) client.socket = socket
console.log('User-Client Connected!: IP: ' + clientIP)
socket.on("test",function(data){
console.log("listened");
let ParsedData = url.parse(data);
let responseData = {
UrlScheme:ParsedData,
socketID: clientID,
api_key: ParsedData.pathname
}
console.log(responseData);
console.log(`${TRANSACTIONGETRECEIPT}_RESPONSE`)
socket
.binary(true)
.emit('testlistened', JSON.stringify(responseData))
});
socket.on('disconnect', function (data) {
console.log(clientID + ' has disconnected')
})
})
一切正常,我能够从服务器端和客户端发出并监听。但是我没有找到说明可以从浏览器或邮递员向http://socket.io发送http get或post请求的文档。是否只能从存在套接字客户端的页面发送请求,还是我们也可以发送
这样的请求? http://localhost:8099/api-hastgraph-key/socket.io/?EIO=3&transport=websocket&sid=w0kDs4oGI7SVwB3YAAAC
or http://localhost:8099/socket.io/api-hastgraph-key?EIO=3&transport=websocket&sid=w0kDs4oGI7SVwB3YAAAC
从浏览器并连接到套接字。
谢谢。任何帮助都将受到高度赞赏。