<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[Серый форум &mdash; WSH,JS: Пример использования WebSocket в WSH]]></title>
		<link>http://forum.script-coding.com/viewtopic.php?id=16506</link>
		<atom:link href="http://forum.script-coding.com/extern.php?action=feed&amp;tid=16506&amp;type=rss" rel="self" type="application/rss+xml" />
		<description><![CDATA[Недавние сообщения в теме «WSH,JS: Пример использования WebSocket в WSH».]]></description>
		<lastBuildDate>Wed, 28 Jul 2021 16:33:47 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[WSH,JS: Пример использования WebSocket в WSH]]></title>
			<link>http://forum.script-coding.com/viewtopic.php?pid=149075#p149075</link>
			<description><![CDATA[<p>Всем привет ! На днях пришлось собрать для одной из задач. Решил поделиться примером.</p><div class="codebox"><pre><code>
// Restarting the process to run it console mode
if(!/cscript\.exe/ig.test(WScript.FullName)){
	new ActiveXObject(&#039;WScript.Shell&#039;).Run(&#039;cmd /c chcp 1251 &gt; nul &amp; cscript //nologo &quot;&#039; + WScript.ScriptFullName + &#039;&quot; &amp; pause&#039;,1);
	WSH.Quit();
}
// Creating WSH standard streams short links
	var	StdIn = WSH.StdIn,
	StdOut = WSH.StdOut,
	StdErr = WSH.StdErr,
	// HTML document creation
	document = new ActiveXObject(&#039;htmlfile&#039;);
// Initializing document as maximum available version
document.write(&#039;&lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge,chrome=1&quot; /&gt;&#039;);
// Getting window object link
var window = document.parentWindow,
	// JSON object
	JSON = window.JSON,
	// Getting WebSocket constructor
	WebSocket = window.WebSocket;
// Checking document mode version
if(document.documentMode &lt; 10) throw new Error(&#039;Sorry, Your browser document mode is &#039; + document.documentMode + &#039;. It doesn\&#039;t support needed functionality !&#039;);
// Checking if needed objects are available
if(!JSON) throw new Error(&#039;Failed to get JSON object&#039;);
if(!WebSocket) throw new Error(&#039;Failed to get WebSocket constructor&#039;);
// Attaching error message
window.onerror = function(message, url, line, column, error){
	throw new Error([
		&#039;HTML document script error&#039;,
		&#039;message:\t&#039; + message,
		&#039;url:\t\t&#039; + url,
		&#039;line:\t\t&#039; + line,
		&#039;column:\t\t&#039; + column,
		&#039;error:\t\t&#039; + error
	].join(&#039;\r\n&#039;))
}
// Creating web socket
var url = &#039;wss://echo.websocket.org/&#039;;
StdOut.WriteLine(&#039;Opening web socket &quot;&#039; + url + &#039;&quot; ...&#039;);
var ws = new WebSocket(url);
// Attaching open event
ws.onopen = function(event){
	StdOut.WriteLine(&#039;Web socket opened !&#039;);
	sendMessage();
}
// Macro for sending messages
function sendMessage(){
	WSH.Sleep(100);
	StdOut.Write(&#039;Send message: &#039;);
	ws.send(StdIn.ReadLine());
}
// Attaching web socket message event
ws.onmessage = function(event){
	StdOut.WriteLine(&#039;Web socket message: &#039; + event.data);
	sendMessage();
}
// Attaching web socket error event
ws.onerror = function(event){
	StdErr.WriteLine(&#039;Web socket error !&#039;);
}
// Attaching web socket close event
ws.onclose = function(event){
	StdOut.WriteLine(&#039;Web socket closed&#039;);
	WSH.Quit();
}
// Main loop for waiting events
while(-1){
	WSH.Sleep(100);		
}
</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (Xameleon)]]></author>
			<pubDate>Wed, 28 Jul 2021 16:33:47 +0000</pubDate>
			<guid>http://forum.script-coding.com/viewtopic.php?pid=149075#p149075</guid>
		</item>
	</channel>
</rss>
