在三星智能电视上使用 WebSockets [英] Using WebSockets on Samsung Smart TV
问题描述
我的要求是在我的三星智能电视应用程序中有一个侦听套接字,以便从同一本地网络中的设备接收事件.
My requirement is to have a listening socket in my Samsung Smart TV app, in order to receive events from a device in the same local network.
我一直在网上搜索实现此目的的方法,我遇到了诸如 Node.js、Socket.io、websocket 之类的术语>.尽管我在网络开发方面理解这些术语(我认为),但我无法想象在我的三星智能电视应用程序中打开监听插座的方法.
I've been searching the web for methods to do that and I came across terms like Node.js, Socket.io, websocket. Even though i understand these terms in terms of web development (I think), I am unable to picture a method to open a listening socket in my Samsung Smart Tv App.
只是为了玩玩,我在 iOS 上使用 GCD 异步套接字编写了一个 TCP 服务器代码和想到将它连接到智能电视并发送欢迎信息.这是我智能电视上的代码 -
Just for the sake of playing around I wrote a TCP Server code on iOS using GCD Async Sockets and thought of connecting it to the smart tv and send a welcome message. This is the code on my smart tv -
//var wsUri = "wss://echo.websocket.org/";
var wsUri = "ws://192.168.1.116:9898/";
//var output;
var webSocketObj={};
webSocketObj.init = function()
{
//output = document.getElementById("output");
this.testWebSocket();
};
webSocketObj.testWebSocket = function()
{
websocket = new WebSocket(wsUri);
websocket.onopen = function(evt) { onOpen(evt); };
websocket.onclose = function(evt) { onClose(evt); };
websocket.onmessage = function(evt) { onMessage(evt); };
websocket.onerror = function(evt) { onError(evt); };
};
function onOpen(evt)
{
writeToScreen("CONNECTED");
doSend("WebSocket rocks");
}
function onClose(evt)
{
writeToScreen("DISCONNECTED");
}
function onMessage(evt)
{
writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data+'</span>');
websocket.close();
}
function onError(evt)
{
writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
}
function doSend(message)
{
writeToScreen("SENT: " + message);
websocket.send(message);
}
function writeToScreen(message)
{
/* var pre = document.createElement("p");
pre.style.wordWrap = "break-word";
pre.innerHTML = message;
output.appendChild(pre);*/
alert('SOCKET HELPER SAYS : '+message);
}
我有一个按钮,我在单击按钮时调用 webSocketObj.init().
I have a button and I'm calling webSocketObj.init() on the button click.
我的服务器 didReadData
日志:
客户端说:GET/HTTP/1.1
Client says : GET / HTTP/1.1
从 SmartTv 登录:
Log from SmartTv :
[JS ALERT]: Scenewebsocket.handleKeyDown(29443)
[JS ERROR]:
File: file://
Line No: 0
Error Detail:
[JS ALERT]: SOCKET HELPER SAYS : DISCONNECTED
还 我尝试将消息从服务器回传到智能电视.这次我得到了日志
ALSO I tried echoing the message back to the Smart Tv from the server. And this time i got the logs
Client says : GET / HTTP/1.1
Client says : Upgrade: WebSocket
Client says : Connection: Upgrade
Client says : Host: 192.168.1.116:9898
Client says : Origin: file://
Client says : Sec-WebSocket-Key1: 1504l73 8Ew/J 4 ,L7W6
Client says : Sec-WebSocket-Key2: TK2 81d A64Bo7 118 0
我知道我做错了什么……这是什么?请帮忙.
I know i'm doing something horribly wrong...what is it? Pls help.
推荐答案
你试过 socket.io 服务器和客户端图书馆?
Have you tried socket.io server and client libraries?
我们在 node.js 上有 socket.io 服务器,电视可以使用 socket.io-client 库连接到它.在 2012 年的电视上,它使用 websocket.在 2011 年的电视上,它使用 XHR 回退.
We have socket.io server on node.js, and TV can connect to it using socket.io-client library. On 2012 TV it uses websocket. On 2011 TV it uses XHR fallback.
这篇关于在三星智能电视上使用 WebSockets的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!