-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle-chrome-websocket.html
50 lines (42 loc) · 1.43 KB
/
google-chrome-websocket.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
var state;
// Check if the browser supports WebSockets
if ("WebSocket" in window) {
// Connect to the WebSocket
var ws = new WebSocket("ws://localhost:1337/p5websocket");
// Listen for messages from WebSocket server
ws.onmessage = function (event) {
state = event.data;
}
} else {
// Alert if WebSocket is not supported
alert("WebSocket NOT supported by your Browser!");
}
// Start speech recognition (supported only in Chrome)
var recognition = new webkitSpeechRecognition();
// Set language and continuous recognition
recognition.lang = "da"; // US-english
recognition.continuous = true;
// When speech recognition receives a result
recognition.onresult = function(event) {
// Get the current transcript
var transcript = event.results[event.results.length-1][0].transcript;
// Send transcript via WebSocket if state is "active"
if (state === "active") {
ws.send(transcript);
}
}
// Restart recognition on timeout
recognition.onend = function(){
recognition.start();
}
// Start the recognition
recognition.start();
</script>
</head>
<body>
</body>
</html>